This repository has been archived on 2023-09-13. You can view files and clone it, but cannot push or open issues or pull requests.
station_obscurum_unity/Assets/Scripts/Enemies/SwarmAnimator.cs

66 lines
1.6 KiB
C#

using UnityEngine;
using UnityEngine.VFX;
namespace Enemy
{
[ExecuteAlways]
public class SwarmAnimator : MonoBehaviour
{
[SerializeField] private VisualEffect vfx;
[SerializeField] private Rigidbody rb;
//[SerializeField]
//private string parameterName = "DeltaVector";
[SerializeField] private Vector3 avoidancePosDefault;
[SerializeField] private float explodeDuration = 1.0f;
private Vector3 currentPosition;
private float dur;
private bool isExploding;
private Vector3 previousPosition;
// Start is called before the first frame update
private void Start()
{
}
// Update is called once per frame
private void Update()
{
previousPosition = currentPosition;
currentPosition = transform.position;
var velocity = currentPosition - previousPosition;
vfx.SetVector3("DeltaVector", velocity);
if (Input.GetKeyDown(KeyCode.Space)) Explode();
if (isExploding)
{
if (dur > explodeDuration) StopExplode();
dur += Time.deltaTime;
}
}
private void FixedUpdate()
{
}
private void Explode()
{
vfx.SetVector3("Avoid", transform.position);
isExploding = true;
dur = 0;
//Wait a sec then stop.
}
private void StopExplode()
{
vfx.SetVector3("Avoid", avoidancePosDefault);
isExploding = false;
vfx.Stop();
}
}
}