| | | 1 | | using TMPro; |
| | | 2 | | using UnityEngine; |
| | | 3 | | using UnityEngine.AI; |
| | | 4 | | |
| | | 5 | | public class EnemyAi : MonoBehaviour |
| | | 6 | | { |
| | | 7 | | public NavMeshAgent agent; |
| | | 8 | | |
| | | 9 | | public Transform player; |
| | | 10 | | |
| | | 11 | | public LayerMask whatIsGround, whatIsPlayer; |
| | | 12 | | |
| | | 13 | | public float forceup; |
| | | 14 | | public float forceforward; |
| | | 15 | | |
| | | 16 | | public Transform Attackpoint; |
| | | 17 | | |
| | | 18 | | public float health; |
| | | 19 | | |
| | | 20 | | public AudioManager AudioManager; |
| | | 21 | | |
| | | 22 | | public GameObject self; |
| | | 23 | | public Transform startpoint; |
| | | 24 | | public TextMeshPro TextMeshPro; |
| | | 25 | | |
| | | 26 | | //Patroling |
| | | 27 | | public Vector3 walkPoint; |
| | | 28 | | bool walkPointSet; |
| | | 29 | | public float walkPointRange; |
| | | 30 | | |
| | | 31 | | //Attacking |
| | | 32 | | public float timeBetweenAttacks; |
| | | 33 | | bool alreadyAttacked; |
| | | 34 | | public GameObject projectile; |
| | | 35 | | |
| | | 36 | | //States |
| | | 37 | | public float sightRange, attackRange; |
| | | 38 | | public bool playerInSightRange, playerInAttackRange; |
| | | 39 | | |
| | 20 | 40 | | void Start () { |
| | 20 | 41 | | UnityEngine.AI.NavMeshAgent agent = GetComponent<UnityEngine.AI.NavMeshAgent>(); |
| | | 42 | | |
| | 20 | 43 | | self.transform.position = startpoint.position; |
| | | 44 | | |
| | 20 | 45 | | } |
| | | 46 | | private void Awake() |
| | 20 | 47 | | { |
| | 20 | 48 | | AudioManager = FindObjectOfType<AudioManager>(); |
| | 20 | 49 | | player = GameObject.Find("Camera Offset").transform; |
| | 20 | 50 | | agent = GetComponent<NavMeshAgent>(); |
| | 20 | 51 | | alreadyAttacked = false; |
| | 20 | 52 | | } |
| | | 53 | | |
| | | 54 | | private void Update() |
| | 44985 | 55 | | { |
| | 44985 | 56 | | TextMeshPro.text = "Health: " + (health); |
| | | 57 | | |
| | | 58 | | //Check for sight and attack range |
| | 44985 | 59 | | playerInSightRange = Physics.CheckSphere(transform.position, sightRange, whatIsPlayer); |
| | 44985 | 60 | | playerInAttackRange = Physics.CheckSphere(transform.position, attackRange, whatIsPlayer); |
| | | 61 | | |
| | 45519 | 62 | | if(agent.velocity.magnitude < 0.1f && !playerInAttackRange && !playerInAttackRange) SearchWalkPoint(); |
| | | 63 | | |
| | 88189 | 64 | | if (!playerInSightRange && !playerInAttackRange) Patroling(); |
| | 45570 | 65 | | if (playerInSightRange && !playerInAttackRange) ChasePlayer(); |
| | 46181 | 66 | | if (playerInAttackRange && playerInSightRange) AttackPlayer(); |
| | 44985 | 67 | | } |
| | | 68 | | |
| | | 69 | | private void Patroling() |
| | 43204 | 70 | | { |
| | 43235 | 71 | | if (!walkPointSet) SearchWalkPoint(); |
| | | 72 | | |
| | 43204 | 73 | | if (walkPointSet) |
| | 43192 | 74 | | agent.SetDestination(walkPoint); |
| | | 75 | | |
| | 43204 | 76 | | Vector3 distanceToWalkPoint = transform.position - walkPoint; |
| | | 77 | | |
| | | 78 | | //Walkpoint reached |
| | 43204 | 79 | | if (distanceToWalkPoint.magnitude < 1f) |
| | 6 | 80 | | walkPointSet = false; |
| | 43204 | 81 | | } |
| | | 82 | | private void SearchWalkPoint() |
| | 565 | 83 | | { |
| | | 84 | | //Calculate random point in range |
| | 565 | 85 | | float randomZ = Random.Range(-walkPointRange, walkPointRange); |
| | 565 | 86 | | float randomX = Random.Range(-walkPointRange, walkPointRange); |
| | | 87 | | |
| | 565 | 88 | | walkPoint = new Vector3(transform.position.x + randomX, transform.position.y, transform.position.z + randomZ); |
| | | 89 | | |
| | 565 | 90 | | if (Physics.Raycast(walkPoint, -transform.up, 2f, whatIsGround)) |
| | 123 | 91 | | walkPointSet = true; |
| | 565 | 92 | | } |
| | | 93 | | |
| | | 94 | | private void ChasePlayer() |
| | 585 | 95 | | { |
| | 585 | 96 | | agent.SetDestination(player.position); |
| | | 97 | | |
| | 585 | 98 | | transform.LookAt(player, Vector3.up); |
| | 585 | 99 | | transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles.x*0, transform.rotation.eulerAngles.y, tran |
| | 585 | 100 | | } |
| | | 101 | | |
| | | 102 | | private void AttackPlayer() |
| | 1196 | 103 | | { |
| | | 104 | | |
| | | 105 | | //Make sure enemy doesn't move |
| | 1196 | 106 | | agent.SetDestination(this.agent.transform.position); |
| | | 107 | | |
| | 1196 | 108 | | agent.acceleration = 0f; |
| | | 109 | | |
| | 1196 | 110 | | transform.LookAt(player, Vector3.up); |
| | 1196 | 111 | | transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles.x*0, transform.rotation.eulerAngles.y, tran |
| | | 112 | | |
| | | 113 | | |
| | 1196 | 114 | | if (!alreadyAttacked) |
| | 22 | 115 | | { |
| | 22 | 116 | | AudioManager.Play("Gun Fire"); |
| | | 117 | | |
| | | 118 | | ///Attack code here |
| | 22 | 119 | | Rigidbody rb = Instantiate(projectile, Attackpoint.position, Quaternion.identity).GetComponent<Rigidbody>(); |
| | 22 | 120 | | rb.AddForce(transform.forward * forceforward, ForceMode.Impulse); |
| | 22 | 121 | | rb.AddForce(transform.up * forceup, ForceMode.Impulse); |
| | | 122 | | ///End of attack code |
| | | 123 | | |
| | 22 | 124 | | alreadyAttacked = true; |
| | 22 | 125 | | Invoke(nameof(ResetAttack), timeBetweenAttacks); |
| | 22 | 126 | | } |
| | 1196 | 127 | | } |
| | | 128 | | private void ResetAttack() |
| | 21 | 129 | | { |
| | 21 | 130 | | alreadyAttacked = false; |
| | 21 | 131 | | } |
| | | 132 | | |
| | | 133 | | |
| | | 134 | | public void TakeDamage(int damage) |
| | 0 | 135 | | { |
| | 0 | 136 | | health -= damage; |
| | | 137 | | |
| | 0 | 138 | | if (health <= 0) Invoke(nameof(DestroyEnemy), 0.5f); |
| | 0 | 139 | | } |
| | | 140 | | private void DestroyEnemy() |
| | 0 | 141 | | { |
| | 0 | 142 | | AudioManager.Play("EnemyDie"); |
| | 0 | 143 | | walkPoint = startpoint.position; |
| | 0 | 144 | | agent.acceleration = 0f; |
| | 0 | 145 | | self.transform.position = startpoint.position; |
| | 0 | 146 | | } |
| | | 147 | | private void OnDrawGizmosSelected() |
| | 0 | 148 | | { |
| | 0 | 149 | | Gizmos.color = Color.red; |
| | 0 | 150 | | Gizmos.DrawWireSphere(transform.position, attackRange); |
| | 0 | 151 | | Gizmos.color = Color.yellow; |
| | 0 | 152 | | Gizmos.DrawWireSphere(transform.position, sightRange); |
| | 0 | 153 | | } |
| | | 154 | | } |