-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpiderWalkerAI.cs
129 lines (116 loc) · 4.64 KB
/
SpiderWalkerAI.cs
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI; //note the extra import to do AI!
/*
* Changelog
* 3/14/23 - Split from NavToPlayer, now handles AI for the big spiders
* 3/28 - Now suicides when out of fuel instead of just vanishing.
* 3/28 - Added animation functionality, script is now very specific to Hunter-Killers.
* 2/23 - stop and resume Navigating functions, deactivating the navmesh and allowing this unit to ragdoll
*
* TODO if split from the navmesh for whatever reason, suicide.
*/
public class SpiderWalkerAI: MonoBehaviour{
[Tooltip("Best if this is the player's center of mass")]
public GameObject player;
public Animator animator;
[Tooltip("The rotator for the turret")]
public GameObject turret;
[Tooltip("The turret's shot spawn location")]
public Transform bulletSpawn;
private NavMeshAgent agent;
//False until the spider finishes deploying.
private bool isNavigating = false;
private EnemySettings enemySettings;
void Start(){
if (player == null) {
player = GameObject.FindGameObjectWithTag("Player").transform.root.Find("CenterOfMass").gameObject;
}
agent = GetComponent<NavMeshAgent>();
enemySettings = EnemySettings.getInstance();
StartCoroutine("activateMovement");
}
void Update(){
if (!agent.isActiveAndEnabled || player == null) {
return;
}
agent.SetDestination(player.transform.position);
if (animator.GetBool("Halted") || !isNavigating) {
agent.speed = 0;
} else {
if (isFacingTarget()) {
agent.speed = enemySettings.SpiderRunMoveSpeed;
animator.SetBool("IsFacingTarget", true);
animator.SetBool("IsReadyToWalk", true);
} else {
agent.speed = enemySettings.SpiderTurnMoveSpeed;
animator.SetBool("IsFacingTarget", false);
}
}
// Turret rotates towards target and spider halts when in range.
if(Vector3.Distance(transform.position, player.transform.position) < enemySettings.SpiderEngagementDistance) {
animator.SetBool("Halted", true);
Vector3 lookDirection = player.transform.position - bulletSpawn.position;
Quaternion toTarget = Quaternion.LookRotation(lookDirection);
turret.transform.rotation = Quaternion.RotateTowards(turret.transform.rotation, toTarget, enemySettings.SpiderTurretRotationSpeed * 100f * Time.deltaTime);
} else {
// If not in range, rotate turret to forwards
Quaternion toTarget = Quaternion.LookRotation(transform.forward);
turret.transform.rotation = Quaternion.RotateTowards(turret.transform.rotation, toTarget, enemySettings.SpiderTurretRotationSpeed * 100f * Time.deltaTime);
animator.SetBool("Halted", false);
}
if(agent.isActiveAndEnabled && !agent.isOnNavMesh) {
Debug.Log("off mesh!");
}
}
/// <summary>
/// True if the unit is roughly facing the player target
/// </summary>
/// <returns></returns>
private bool isFacingTarget() {
if(player == null) {
return false;
}
float diff = Vector3.Angle(transform.forward, player.transform.position - transform.position);
if (diff < 10f) {
return true;
}
return false;
}
/// <summary>
/// Disable the navmesh, make the unit non-kinematic
/// </summary>
public void stopNavigating() {
isNavigating = false;
agent.enabled = false;
animator.SetBool("Halted", true);
Rigidbody rb = GetComponent<Rigidbody>();
if (rb != null) {
rb.isKinematic = false;
}
}
/// <summary>
/// Enable the navmesh, make the unit kinematic
/// </summary>
public void resumeNavigating() {
isNavigating = true;
agent.enabled = true;
animator.SetBool("Halted", false);
Rigidbody rb = GetComponent<Rigidbody>();
if (rb != null) {
rb.isKinematic = true;
}
}
IEnumerator activateMovement() {
yield return new WaitForSeconds(enemySettings.SpiderStartMoveDelay);
if (isFacingTarget()) {
animator.SetBool("IsFacingTarget", true);
animator.SetBool("IsReadyToWalk", true);
isNavigating = true;
} else {
animator.SetBool("IsReadyToTurn", true);
isNavigating = true;
}
}
}