Dong_Minh_Doxygen: D:/Google Drive/Unity Projects/Tank Game/Assets/Scripts/EnemyAI.cs Source File

Dong Minh

Dong_Minh_Doxygen
EnemyAI.cs
Go to the documentation of this file.
1 using System.Collections;
2 using System.Collections.Generic;
3 using UnityEngine;
4 
5 public class EnemyAI : MonoBehaviour {
6 
7  public Transform Player; // prefab for the player
8  public float playerDistance; // will be measured later to check for distance
9  public float rotationDamping; // how dampened the rotation should be
10 
11  public GameObject bulletPrefab;
12  public Transform bulletEmitter;
13  public float FireSpeed; // how fast the bullet should travel?
14  public float FireFrequency; // how often they will fire?
15  RaycastHit target;
16 
17  private float timer = 0;
18 
19  public const int maxHealth = 3;
20  public int currentHealth = maxHealth;
21 
32  void Update () {
34  }
35 
55  void OnCollisionEnter (Collision collision){
56 
57  if (GameObject.FindGameObjectWithTag ("playerBullet")) {
58  Destroy (collision.collider.gameObject);
59  currentHealth -= 1;
60  }
61 
62  if (currentHealth == 0)
63  Destroy(gameObject);
64  }
65 
83  playerDistance = Vector3.Distance (Player.position, transform.position);
84 
85  timer += Time.deltaTime;
86 
87  if (playerDistance < 150f) {
88  lookAtPlayer ();
89  Debug.Log ("Looking at player!");
90  }
91 
92  if (playerDistance < 75f) {
93  if (timer > FireFrequency) {
94  FireAtPlayer ();
95  timer = 0;
96  }
97  Debug.Log ("Firing at the player!");
98  }
99  }
100 
111  void lookAtPlayer(){
112  Quaternion rotation = Quaternion.LookRotation (Player.position - transform.position);
113  transform.rotation = Quaternion.Slerp (transform.rotation, rotation, Time.deltaTime * rotationDamping);
114  }
115 
128  void FireAtPlayer(){
129  GameObject BulletInstance = Instantiate(bulletPrefab, bulletEmitter.position, bulletEmitter.rotation) as GameObject;
130  Rigidbody launch = BulletInstance.GetComponent<Rigidbody>();
131  launch.velocity = FireSpeed * bulletEmitter.forward;
132  Destroy (BulletInstance, 6f);
133  }
134 }
void FireAtPlayer()
Fire at the player
Definition: EnemyAI.cs:128
void lookAtPlayer()
Extensive math problem here ti determine the player position and enemy position.
Definition: EnemyAI.cs:111
float rotationDamping
Definition: EnemyAI.cs:9
float timer
Definition: EnemyAI.cs:17
float playerDistance
Definition: EnemyAI.cs:8
RaycastHit target
Definition: EnemyAI.cs:15
Transform bulletEmitter
Definition: EnemyAI.cs:12
void Update()
Update every per frame. Only function in here is the DistanceToLookAt.
Definition: EnemyAI.cs:32
Transform Player
Definition: EnemyAI.cs:7
void OnCollisionEnter(Collision collision)
When there is a collision detected by the player bullet, the enemy&#39;s life will go down...
Definition: EnemyAI.cs:55
float FireSpeed
Definition: EnemyAI.cs:13
GameObject bulletPrefab
Definition: EnemyAI.cs:11
const int maxHealth
Definition: EnemyAI.cs:19
int currentHealth
Definition: EnemyAI.cs:20
float FireFrequency
Definition: EnemyAI.cs:14
void DistanceToLookAt()
Determine a function of how far the enemy looks at the player (distance), and when to shoot at the pl...
Definition: EnemyAI.cs:82
Generated by   doxygen 1.8.13