Files
Inter-Face-Off/Assets/Scripts/EnemyController.cs
2026-01-22 11:43:00 +00:00

60 lines
1.6 KiB
C#

using System.Collections;
using UnityEngine;
using Random = UnityEngine.Random;
namespace InterfaceOff.WorldScene
{
public class EnemyController : MonoBehaviour
{
[SerializeField] private PlayerController Player;
[SerializeField] private int DeathIndex = 500;
[SerializeField] private Rigidbody Body;
[SerializeField] private GameObject RendererObject;
private Quaternion StartRotation;
private Vector3 StartPosition;
private bool WasAliveLastFrame;
private bool Alive = true;
private void Start()
{
StartPosition = transform.position;
StartRotation = transform.rotation;
Body.Sleep();
}
private void Update()
{
/* Updates the death state */
WasAliveLastFrame = Alive;
Alive = DeathIndex >= Player.FrameIndex;
/* Checks if it just died */
if (!Alive && WasAliveLastFrame)
{
Body.WakeUp();
Body.AddForce(Random.insideUnitSphere * 250);
StartCoroutine(routine: Suicide());
}
if (Alive)
{
transform.position = StartPosition;
transform.rotation = StartRotation;
Body.velocity = Vector3.zero;
RendererObject.SetActive(true);
Body.Sleep();
}
}
private IEnumerator Suicide()
{
yield return new WaitForSeconds(1f);
RendererObject.SetActive(false);
}
}
}