-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnemy.cs
127 lines (107 loc) · 4.16 KB
/
Enemy.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
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Enemy : MonoBehaviour
{
//@script RequireComponent(Rigidbody);
public static bool walking = true;
public static bool running = false;
public static bool died = false;
public GameObject player;
public GameObject rock;
float rotateEnemy;
Vector3 positionRock;
Vector3 lookPosition;
float timeStopped = 0f;
float timeToChangeWay = 0f;
float randomRotate = 0f;
public int life = 100;
public Sprite[] iconEnemy = new Sprite[12];
public Image imageEnemyHUD;
public RectTransform lifeHUD;
// Use this for initialization
void Start ()
{
life = 100;
died = false;
player = GameObject.Find ("/Hercules");
lifeHUD = GameObject.Find ("/Canvas/HUD/Enemy Life Bar").GetComponent<RectTransform>();
imageEnemyHUD = GameObject.Find ("/Canvas/HUD/Enemy Image").GetComponent<Image>();
}
// Use this for physics situations
void FixedUpdate () {
if (walking == true)
{
//Rotate (randomRotate);
if (Player.died == false && player.transform.position.x - transform.position.x < 10 && player.transform.position.y - transform.position.y < 10 && player.transform.position.z - transform.position.z < 10 && player.transform.position.x - transform.position.x > -10 && player.transform.position.y - transform.position.y > -10 && player.transform.position.z - transform.position.z > -10)
{
transform.LookAt (lookPosition);
if (player.transform.position.x - transform.position.x > 1 && player.transform.position.y - transform.position.y > 1 && player.transform.position.z - transform.position.z > 1 && player.transform.position.x - transform.position.x < -1 && player.transform.position.y - transform.position.y < -1 && player.transform.position.z - transform.position.z < -1)
rigidbody.velocity = transform.forward * 150 * Time.deltaTime;
running = true;
}else
{
rigidbody.velocity = transform.forward * 60 * Time.deltaTime;
timeToChangeWay += Time.deltaTime;
running = false;
}
}
}
// Update is called once per frame
void Update ()
{
imageEnemyHUD.sprite = iconEnemy[Menu.numLevel];
lifeHUD.sizeDelta = new Vector2 (life*2, 20);
lookPosition.x = player.transform.position.x;
lookPosition.y = 0;
lookPosition.z = player.transform.position.z;
if (Menu.numLevel == 1)
{
if (Items.rockCollision == true && transform.position.x - rock.transform.position.x <= rock.GetComponent<AudioSource>().minDistance && transform.position.y - rock.transform.position.y <= rock.GetComponent<AudioSource>().minDistance && transform.position.z - rock.transform.position.z <= rock.GetComponent<AudioSource>().minDistance && transform.position.x - rock.transform.position.x >= -rock.GetComponent<AudioSource>().minDistance && transform.position.y - rock.transform.position.y >= -rock.GetComponent<AudioSource>().minDistance && transform.position.z - rock.transform.position.z >= -rock.GetComponent<AudioSource>().minDistance)
{
positionRock.x = rock.transform.position.x;
positionRock.y = rock.transform.position.y;
positionRock.z = rock.transform.position.z;
transform.LookAt(rock.transform);
Lerp (positionRock);
}else
Items.rockCollision = false;
}
if (timeToChangeWay > 10)
{
walking = false;
timeStopped += Time.deltaTime;
}
if (timeStopped > 3)
{
randomRotate = Random.value * 100;
walking = true;
transform.Rotate(0,randomRotate,0);
timeToChangeWay = 0;
timeStopped = 0;
}
if (life <= 0)
{
died = true;
Destroy (gameObject);
}
}
void OnTriggerEnter (Collider other) {
if (other.gameObject.tag == "Weapon" && Player.attacked == true)
{
life -= 5;
}
}
void Rotate (float valueRotate) {
Quaternion quaternion;
quaternion.y = valueRotate;
rotateEnemy = Quaternion.Lerp (transform.rotation, quaternion, Time.deltaTime).eulerAngles.y;
transform.rotation = new Quaternion (transform.rotation.eulerAngles.x, rotateEnemy, transform.rotation.eulerAngles.z, 0);
}
void Lerp (Vector3 position) {
walking = true;
transform.position = Vector3.Lerp(transform.position, position, Time.deltaTime);
if (transform.position == position)
walking = false;
}
}