-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnemyController.cs
141 lines (125 loc) · 4.63 KB
/
EnemyController.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Rigidbody2D))]
[RequireComponent(typeof(BoxCollider2D))]
[RequireComponent(typeof(HealthManager))]
[RequireComponent(typeof(CharacterStats))]
[RequireComponent(typeof(DamagePlayer))]
[RequireComponent(typeof(QuestEnemy))]
public class EnemyController : MonoBehaviour
{
[Tooltip("Velodcidad de movimiento")]
public float speed = 1;
private Rigidbody2D _rigidbody;
private bool isMoving;
private bool wasHit;
private bool playerWasHit;
[Tooltip("Tiempo que tarda el enemigo entre pasos sucesivos")]
public float timeBetweenSteps;
private float timeBetweenStepsCounter;
[Tooltip("Tiempo que tarda el enemigo en dar un paso")]
public float timeToMakeStep;
private float timeToMakeStepCounter;
public Vector2 directionToMove;
private Transform thePlayer;
//Animacion
private Animator _animator;
private int xPos;
private int yPos;
// Start is called before the first frame update
void Start()
{
thePlayer = GameObject.FindWithTag("Player").GetComponent<Transform>();
_animator = GetComponent<Animator>();
_rigidbody = GetComponent<Rigidbody2D>();
//Para que arranque moviendose
timeBetweenStepsCounter = timeBetweenSteps * Random.Range(0.5f, 1.5f);
timeToMakeStepCounter = timeToMakeStep * Random.Range(0.5f, 1.5f);
}
private void OnTriggerStay2D(Collider2D collision)
{
if (collision.gameObject.tag.Equals("Player"))
{
if (wasHit)
{
isMoving = true;
this.transform.position = Vector2.MoveTowards(this.transform.position, thePlayer.position, -4* speed * Time.fixedDeltaTime);
//directionToMove = this.transform.position - thePlayer.position;
_animator.SetFloat("Horizontal", directionToMove.x);
_animator.SetFloat("Vertical", directionToMove.y);
return;
}
if (playerWasHit)
{
isMoving = true;
this.transform.position = Vector2.MoveTowards(this.transform.position, thePlayer.position, -2 * speed * Time.fixedDeltaTime);
directionToMove = this.transform.position - thePlayer.position;
_animator.SetFloat("Horizontal", directionToMove.x);
_animator.SetFloat("Vertical", directionToMove.y);
return;
}
//Debug.Log("Player entró en la zona de vision");
isMoving = true;
this.transform.position = Vector2.MoveTowards(this.transform.position, thePlayer.position, speed * Time.fixedDeltaTime);
directionToMove = thePlayer.position - this.transform.position;
_animator.SetFloat("Horizontal", directionToMove.x);
_animator.SetFloat("Vertical", directionToMove.y);
}
}
private void OnTriggerExit2D(Collider2D collision)
{
if (collision.gameObject.tag.Equals("Player"))
{
isMoving = false;
wasHit = false;
playerWasHit = false;
}
}
// Update is called once per frame
void Update()
{
if (isMoving)
{
timeToMakeStepCounter -= Time.deltaTime;
_rigidbody.velocity = directionToMove * speed;
//Cuando me quedo sin tiempo de movimeinto paramos al enemigo
if(timeToMakeStepCounter < 0)
{
isMoving = false;
timeBetweenStepsCounter = timeBetweenSteps;
_rigidbody.velocity = Vector2.zero;
}
}
else
{
timeBetweenStepsCounter -= Time.deltaTime;
//Cuando me quedo sin tiempo de estar parado
if(timeBetweenStepsCounter < 0)
{
isMoving = true;
timeToMakeStepCounter = timeToMakeStep;
directionToMove = new Vector2(Random.Range(-1,2), Random.Range(-1,2));
_animator.SetFloat("Horizontal", directionToMove.x);
_animator.SetFloat("Vertical", directionToMove.y);
}
}
}
/*
private void LateUpdate()
{
_animator.SetBool("isWalking", isMoving);
_animator.SetFloat("Horizontal", directionToMove.x);
_animator.SetFloat("Vertical", directionToMove.y);
_animator.SetFloat("LastV", directionToMove.x);
_animator.SetFloat("LastH", directionToMove.y);
}*/
public void EnemyWasHit()
{
wasHit = true;
}
public void HitThePlayer()
{
playerWasHit = true;
}
}