-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnemyTurretController.cs
More file actions
70 lines (59 loc) · 2.62 KB
/
EnemyTurretController.cs
File metadata and controls
70 lines (59 loc) · 2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/*
* Changelog: 3/8: Initial commit
*/
public class EnemyTurretController : MonoBehaviour{
[Tooltip("The part that actually spins to face target, should be a child")]
public GameObject rotator;
[Tooltip("Location where shots come out of")]
public Transform bulletSpawn;
[Tooltip("Bullet created by turret (bullet mode only)")]
public GameObject bulletPrefab;
private GameObject target;
private Transform targetCenterOfMass;
private EnemySettings enemySettings;
void Start() {
enemySettings = EnemySettings.getInstance();
StartCoroutine("fireWeapon");
}
void Update() {
if (target != null) {
Vector3 lookDirection = targetCenterOfMass.position - bulletSpawn.position;
Quaternion toTarget = Quaternion.LookRotation(lookDirection);
rotator.transform.rotation = Quaternion.Slerp(rotator.transform.rotation, toTarget, enemySettings.TurretRotationSpeed * Time.deltaTime);
}
}
//Rangefinder should be on a layer that only collides with the player
private void OnTriggerStay(Collider other) {
//Has a target already, rangefinder doesn't need to do anything
if (target != null) {
return;
}
if (other.transform.root.CompareTag("Player")) {
target = other.transform.root.gameObject;
targetCenterOfMass = other.transform.root.Find("CenterOfMass");
}
}
private void OnTriggerExit(Collider other) {
if (other.transform.root.CompareTag("Player")) {
target = null;
}
}
IEnumerator fireWeapon() {
while (true) {
yield return new WaitForSeconds(enemySettings.TurretAttackDelay);
if (target != null) {
float accuracy = enemySettings.TurretShotErrorRate;
// Get shot imprecision vector.
Vector3 imprecision = Random.Range(-accuracy, accuracy) * bulletSpawn.right;
imprecision += Random.Range(-accuracy, accuracy) * bulletSpawn.up;
// Get shot desired direction. Adjust up slightly to not shoot at the character's feet
Vector3 shotDirection = targetCenterOfMass.position - bulletSpawn.position;
GameObject bullet = Instantiate(bulletPrefab, bulletSpawn.position, Quaternion.identity);
bullet.GetComponent<Rigidbody>().AddForce((shotDirection.normalized + imprecision) * enemySettings.TurretBulletForce);
}
}
}
}