Initial Commit
This commit is contained in:
92
Assets/Scripts/EnemyController.cs
Normal file
92
Assets/Scripts/EnemyController.cs
Normal file
@@ -0,0 +1,92 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.AI;
|
||||
|
||||
public class EnemyController : MonoBehaviour
|
||||
{
|
||||
public float moveSpeed;
|
||||
public float targetRange;
|
||||
public float attackRange;
|
||||
public float attackSpeed;
|
||||
public float attackDamage;
|
||||
public float health;
|
||||
public Transform defaultTarget;
|
||||
public Transform target;
|
||||
private NavMeshAgent navMeshAgent;
|
||||
private Animator animator;
|
||||
private SphereCollider targetCollider;
|
||||
private float attackCooldown;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
navMeshAgent = GetComponent<NavMeshAgent>();
|
||||
animator = GetComponentInChildren<Animator>();
|
||||
targetCollider = GetComponentInChildren<SphereCollider>();
|
||||
navMeshAgent.speed = moveSpeed;
|
||||
targetCollider.radius = targetRange;
|
||||
}
|
||||
|
||||
void Start()
|
||||
{
|
||||
MoveToDefault();
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
if(target != null && !target.gameObject.activeInHierarchy)
|
||||
{
|
||||
target = null;
|
||||
MoveToDefault();
|
||||
}
|
||||
|
||||
if(target != null)
|
||||
Attack();
|
||||
|
||||
animator.SetFloat("Speed", navMeshAgent.velocity.magnitude);
|
||||
}
|
||||
|
||||
private void OnTriggerEnter(Collider other)
|
||||
{
|
||||
if(target != null) return;
|
||||
if(!other.CompareTag("Gate")) return;
|
||||
|
||||
AttackPoint attackPoint = other.GetComponentInChildren<AttackPoint>();
|
||||
if(attackPoint != null)
|
||||
{
|
||||
target = attackPoint.transform;
|
||||
navMeshAgent.stoppingDistance = attackRange;
|
||||
navMeshAgent.destination = attackPoint.transform.position;
|
||||
}
|
||||
}
|
||||
|
||||
private void MoveToDefault()
|
||||
{
|
||||
navMeshAgent.stoppingDistance = 0f;
|
||||
if(defaultTarget != null)
|
||||
navMeshAgent.destination = defaultTarget.position;
|
||||
}
|
||||
|
||||
private void Attack()
|
||||
{
|
||||
attackCooldown -= Time.deltaTime;
|
||||
if(Vector3.Distance(target.position, transform.position) < attackRange && attackCooldown <= 0f)
|
||||
{
|
||||
transform.LookAt(target.position);
|
||||
animator.SetTrigger("Attack");
|
||||
attackCooldown = 1f / attackSpeed;
|
||||
target.GetComponent<AttackPoint>().AddAttacker();
|
||||
target.GetComponentInParent<Building>().TakeDamage(attackDamage);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnDrawGizmosSelected()
|
||||
{
|
||||
// Target range — green if priority target is assigned, red if not
|
||||
Gizmos.color = target != null ? Color.green : Color.red;
|
||||
Gizmos.DrawWireSphere(transform.position, targetRange);
|
||||
|
||||
// Attack range — green if target is within attack range, red if not
|
||||
bool inAttackRange = target != null && Vector3.Distance(target.position, transform.position) < attackRange;
|
||||
Gizmos.color = inAttackRange ? Color.green : Color.red;
|
||||
Gizmos.DrawWireSphere(transform.position, attackRange);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user