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#
Raw Permalink Normal View History

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