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

62 lines
1.4 KiB
C#
Raw Permalink Normal View History

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