-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdusmanAI.cs
144 lines (120 loc) · 3.7 KB
/
dusmanAI.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class dusmanAI : MonoBehaviour
{
public float viewRange;
public float minRange;
public float closeAttackRange;
public float closeAttackTime;
public float bulletRange;
public float bulletTime;
public float movementSpeed;
public float jumpPower;
public float jumpTime;
//public bool isFacingLeft = false;
public GameObject character;
public GameObject bullet;
float closeATime2 = 0;
float bulletTime2 = 0;
float jumpTime2 = 0;
bool grounded = true;
bool pathBlocked = false;
bool StopMoving;
float tempX = 10000;
float tempY = 0;
private Animator animator;
// Update is called once per frame
void Start()
{
animator = GetComponent<Animator>();
}
void Update()
{
if (this.animator.GetCurrentAnimatorStateInfo(0).IsName("Enemy_Attack") || this.animator.GetCurrentAnimatorStateInfo(0).IsName("Enemy_TakeDamage"))
{
StopMoving = true;
}else
{
StopMoving = false;
}
Vector3 karPos = character.transform.position;
Vector3 pos = transform.position;
if (Mathf.Abs(karPos.x - pos.x)< viewRange)
{
if (!StopMoving)
{
moveTowardCharacter(karPos, pos);
}
}
if (Mathf.Abs(karPos.x - pos.x) < closeAttackRange)
{
//closeAttack();
}
else if (Mathf.Abs(karPos.x - pos.x) < bulletRange)
{
// shoot(karPos, pos);
}
if (pos.y != tempY) { grounded = false; }
else { grounded = true; }
}
void moveTowardCharacter(Vector3 karPos, Vector3 pos)
{
if (Mathf.Abs(karPos.x - pos.x) > minRange && !(pathBlocked&&grounded))
{
Rigidbody2D rb2d = gameObject.GetComponent<Rigidbody2D>();
gameObject.GetComponent<Rigidbody2D>().velocity = new Vector2((karPos.x - pos.x) * movementSpeed / Mathf.Abs(karPos.x - pos.x), rb2d.velocity.y);
transform.localScale = new Vector3((karPos.x - pos.x) / Mathf.Abs(karPos.x - pos.x), 1, 1);
//isFacingLeft = false;
}
else if ((pathBlocked && grounded))
{
jump();
}
}
void jump ()
{
if (jumpTime - (Time.realtimeSinceStartup - jumpTime2) <= 0 )
{
jumpTime2 = Time.realtimeSinceStartup;
gameObject.GetComponent<Rigidbody2D>().velocity += new Vector2(0f, jumpPower);
}
}
void closeAttack()
{
//gettingDamage
//animation
if (closeAttackTime - (Time.realtimeSinceStartup - closeATime2) <= 0)
{
Debug.Log("hit");
closeATime2 = Time.realtimeSinceStartup;
}
}
void shoot(Vector3 karPos, Vector3 pos)
{
if (bulletTime - (Time.realtimeSinceStartup - bulletTime2) <= 0)
{
bulletTime2 = Time.realtimeSinceStartup;
Debug.Log("fire");//ate�
Vector3 a = (karPos - pos);
GameObject bulletClone = (GameObject)Instantiate(bullet, pos + transform.right * 0.5f + new Vector3(0, 0, -0.21f), transform.rotation);
bulletClone.transform.up = new Vector3(a.x, a.y, 0);
bulletClone.GetComponent<BulletScriptt>().StartShooting(a.x<0);
}
}
void OnTriggerEnter2D(Collider2D temas)
{
if (temas.tag == "block")
{
pathBlocked = true;
jump();
}
}
void OnTriggerExit2D(Collider2D temas)
{
if (temas.tag == "block")
{
pathBlocked = false;
}
}
}