-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnemyScript.cs
More file actions
109 lines (99 loc) · 2.98 KB
/
Copy pathEnemyScript.cs
File metadata and controls
109 lines (99 loc) · 2.98 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
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyScript : MonoBehaviour
{
public float health = 100;
public Transform Target;
[SerializeField]private float moveSpeed;
[SerializeField]private float rotSpeed = 5;
private Rigidbody rigidbody;
[SerializeField] private float stoppingDist = 1;
[SerializeField] private GameObject deathPS;
public bool dead;
public bool shouldFollow = true;
[SerializeField] private Transform bodyPoint;
public AudioSource hitAudio;
private PlayerScript PScript;
private GameEventsManagerScript GEManager;
private void Awake()
{
GEManager = GameObject.Find("GameEventsManager").GetComponent<GameEventsManagerScript>();
}
private void OnEnable()
{
GEManager.OnGameOver += GameOver;
}
private void OnDisable()
{
GEManager.OnGameOver -= GameOver;
}
void GameOver()
{
Destroy(gameObject);
}
// Start is called before the first frame update
void Start()
{
rigidbody = GetComponent<Rigidbody>();
Target = GameObject.Find("Target").transform;
PScript = Target.root.GetComponent<PlayerScript>();
}
private void Update()
{
if(!dead)
{
if (health <= 0)
Die();
if (shouldFollow)
{
Quaternion lookRotation = Quaternion.LookRotation((Target.position - transform.position).normalized);
transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, rotSpeed * Time.deltaTime);
}
}
}
void Die()
{
rigidbody.useGravity = true;
rigidbody.constraints = RigidbodyConstraints.None;
GetComponent<Animation>().enabled = false;
dead = true;
Invoke("KillFromScene", Random.Range(2, 3));
}
void KillFromScene()
{
Instantiate(deathPS, bodyPoint.position, Quaternion.identity);
Destroy(gameObject);
}
float attackTimer;
// Update is called once per frame
void FixedUpdate()
{
//Vector3 dir = (Target.position - transform.position).normalized;
if (!dead)
{
if (shouldFollow)
{
if (Vector3.Distance(Target.position, transform.position) > stoppingDist)
rigidbody.AddForce(transform.forward * moveSpeed, ForceMode.VelocityChange);
else
{
attackTimer += Time.fixedDeltaTime;
if(attackTimer > Random.Range(2,4))
{
Attack();
attackTimer = 0;
}
}
}
}
}
void Attack()
{
PScript.Damage(10);
}
public void Damage( int val)
{
health -= val;
}
}