-
Notifications
You must be signed in to change notification settings - Fork 0
/
PoppyAiScript.cs
157 lines (137 loc) · 3.51 KB
/
PoppyAiScript.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
using UnityEngine;
using System.Collections;
public class PoppyAiScript : MonoBehaviour
{
public GameObject player;
public Transform myTransform;
public Transform target; //the target
public int damage; //damage amount block characters can do with their melee attack
public string whichTarget; //type of target. Can be player, turret, building, or whatever
public float dist;
public float closeEnough;
public int maxDistance;
public float moveSpeed; //movement speed of enemies
public float moveSpeedManupulator = 1.0f; //if you want to speed up or slow down enemies, change this value
public float turnSpeed; //turn speed of enemies
public float turnSpeedManipulator = 1.0f; //if you want to speed up or slow down enemy rotations
public float timeBetweenAttacks = 0.5f;
PlayerHealth1 playerHealth;
PoppyLife poppylife;
public bool playerInRange;
public float timer;
Animator anim;
Quaternion rotPoint;
Quaternion lookingAt;
public bool chase = false;
public bool searchPlayer = false;
public bool takeDamage;
void Awake ()
{
myTransform = transform;
poppylife = GetComponent<PoppyLife>();
anim = GetComponent<Animator>();
}
void Start ()
{
player = GameMasterObject.playerUse;
playerHealth = player.GetComponent<PlayerHealth1> ();
target = player.transform;
chase = true;
}
void Update ()
{
player = GameMasterObject.playerUse;
target = player.transform;
if(timer < timeBetweenAttacks)
{
timer += Time.deltaTime;
}
if (timer >= timeBetweenAttacks && playerInRange && !poppylife.dead)
{
Attack ();
timer = 0;
}
rotPoint = Quaternion.LookRotation ((target.position + new Vector3(0f, 7f, 0f)) - myTransform.position);
lookingAt = Quaternion.Slerp (myTransform.rotation, rotPoint, turnSpeed * turnSpeedManipulator * Time.deltaTime);
/*if(lookingAt.x > 0)
{
lookingAt.x = 0;
}
if(lookingAt.z > 0)
{
lookingAt.z = 0;
}*/
dist = Vector3.Distance (myTransform.position, target.position);
if (dist <= closeEnough && !poppylife.dead)
{
myTransform.rotation = lookingAt;
if (chase)
{
//anim.SetFloat ("vSpeed", 1);
chase = false;
}
if (dist > maxDistance)
{
//Move Towards target.
//anim.SetBool ("Awake", true);
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
chase = true;
}
else if (dist <= maxDistance)
{
chase = false;
//anim.SetFloat ("vSpeed", 0);
}
}
}
void OnTriggerEnter(Collider other)
{
if(other.gameObject.tag == "Player")
{
playerInRange = true;
}
if(other.gameObject.tag == "Death Flag")
{
playerInRange = false;
}
}
void OnTriggerStay(Collider other)
{
if(other.gameObject.tag == "Player")
{
playerInRange = true;
}
if(other.gameObject.tag == "Death Flag")
{
playerInRange = false;
}
}
void OnTriggerExit(Collider other)
{
if(other.gameObject.tag == "Player")
{
playerInRange = false;
}
}
public void Attack()
{
if(takeDamage)
{
if (playerHealth != null)
{
if(HUDHealthScript.timer > 5)
{
HUDHealthScript.timer = 0;
}
if (playerHealth.currentHealth > 0 && playerHealth.currentHealth <= playerHealth.startingHealth)
{
playerHealth.TakeDamage (damage);
}
else if (playerHealth.currentHealth > 0 && playerHealth.currentHealth >= playerHealth.startingHealth + 1)
{
playerHealth.TakeArmorDamage (damage);
}
}
}
}
}