Files
Inter-Face-Off/Assets/Scripts/EnemyController.cs
2026-01-23 00:24:44 +00:00

65 lines
1.8 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;
[SerializeField] private GameObject ExplosionPrefab;
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);
GameObject explosionGoBoomBoom = Instantiate(ExplosionPrefab, transform);
yield return new WaitForSeconds(1f);
Destroy(explosionGoBoomBoom);
}
}
}