-
Notifications
You must be signed in to change notification settings - Fork 0
/
NavtoPlayer.cs
117 lines (103 loc) · 3.64 KB
/
NavtoPlayer.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI; //note the extra import to do AI!
/*
* Changelog
* 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 NavtoPlayer : MonoBehaviour{
public GameObject player;
public Animator animator;
private NavMeshAgent agent;
//False until the spider finishes deploying.
private bool isNavigating = false;
void Start(){
if (player == null) {
player = GameObject.FindGameObjectWithTag("Player").transform.root.gameObject;
}
agent = GetComponent<NavMeshAgent>();
StartCoroutine("activateMovement");
StartCoroutine("outOfFuel");
}
void Update(){
if (!agent.isActiveAndEnabled || player == null || !isNavigating) {
return;
}
agent.SetDestination(player.transform.position);
if (animator.GetBool("Halted")) {
agent.speed = 0;
return;
}
if (isFacingTarget()) {
animator.SetBool("IsReadyToWalk", true);
animator.SetBool("IsFacingTarget", true);
agent.speed = EnemySettings.getInstance().HunterKillerRunMoveSpeed;
isNavigating = true;
} else {
animator.SetBool("IsFacingTarget", false);
agent.speed = EnemySettings.getInstance().HunterKillerTurnMoveSpeed;
}
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 outOfFuel() {
yield return new WaitForSeconds(EnemySettings.getInstance().HunterKillerLifespan);
stopNavigating();
EnemyExplodeInRange exploder = GetComponentInChildren<EnemyExplodeInRange>();
exploder.explode();
}
IEnumerator activateMovement() {
yield return new WaitForSeconds(EnemySettings.getInstance().HunterKillerStartMoveDelay);
if (animator != null && isFacingTarget()) {
animator.SetBool("IsReadyToWalk", true);
isNavigating = true;
} else {
animator.SetBool("IsReadyToTurn", true);
isNavigating = true;
}
}
}