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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234 | // --------------------------- Unreal Shot 3d / EnemyMovement.cs / Source by lanky ------------------
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(AudioSource))]
public class EnemyMovement : Photon.MonoBehaviour {
public AudioClip audioEnemyRun;
public AudioClip audioEnemyAttack;
public AudioClip audioEnemyDie;
public AudioClip[] audioEnemyRespawn;
HealthEnemy healthEnemy;
private NavMeshAgent Agent; // AI
private Quaternion graczTargetRotation; // przekręć o 180 (korekta)
float dystans = 0;
GameObject gracz;
Animator animEnemy;
bool goFirstUpdate = false;
Vector3 realPosition = Vector3.zero;
Quaternion realRotation = Quaternion.identity;
float realAimAngle = 0;
void Start ()
{
GetComponent<AudioSource>().PlayOneShot(audioEnemyRespawn[Random.Range(0, 3)], 0.5f); // Random 0-2
healthEnemy = GetComponent<HealthEnemy>();
animEnemy = GetComponent<Animator>();
Agent = GetComponent<NavMeshAgent>();
gracz = GameObject.Find("PlayerController(Clone)");
if (gracz == null) Debug.Log("Nie ma gracza!");
//gracz.GetComponent("PlayerController");
}
string nameFirst = null;
bool timeFirst = true;
float respawnTimer = 0;
void audioPlay(string name, float time)
{
if (nameFirst != name)
{
nameFirst = name;
timeFirst = true;
}
if(name == "bieg")
{
if (timeFirst)
{
//Debug.Log("biegnie");
//audio.PlayOneShot(audioEnemyRun, 0.3f);
GetComponent<AudioSource>().clip = audioEnemyRun;
GetComponent<AudioSource>().volume = 0.1f;
GetComponent<AudioSource>().Play();
respawnTimer = time;
timeFirst = false;
}
respawnTimer -= Time.deltaTime;
if (respawnTimer <= 0) timeFirst = true;
} else
if(name == "atak")
{
if (timeFirst)
{
//Debug.Log("atakuje");
//audio.PlayOneShot(audioEnemyAttack, 0.3f);
GetComponent<AudioSource>().clip = audioEnemyAttack;
GetComponent<AudioSource>().volume = 0.5f;
GetComponent<AudioSource>().Play();
respawnTimer = time;
timeFirst = false;
}
respawnTimer -= Time.deltaTime;
if (respawnTimer <= 0)
{
//Debug.Log("hit");
Health playerPV = gracz.transform.GetComponent<Health>();
playerPV.GetComponent<PhotonView>().RPC ("TakeDamage", PhotonTargets.AllBuffered, 25f);
timeFirst = true;
}
} else
if(name == "die")
{
if (timeFirst)
{
//Debug.Log("umiera");
//audio.PlayOneShot(audioEnemyDie, 0.3f);
GetComponent<AudioSource>().clip = audioEnemyDie;
GetComponent<AudioSource>().volume = 0.5f;
GetComponent<AudioSource>().Play();
respawnTimer = time;
timeFirst = false;
}
respawnTimer -= Time.deltaTime;
if (respawnTimer <= 0) timeFirst = false;
} else
{
//Debug.Log("nic nie robi");
}
}
string zdarzenieAudio;
float czasAudio;
bool enemyWraca = false;
Vector3 enemyBaza = new Vector3(0.0f,7.0f,0.5f); // (75.7f,1.0f,2.0f);
void Update ()
{
//Debug.Log(transform.position);
if (enemyWraca)
{
zdarzenieAudio = "bieg";
czasAudio = audioEnemyRun.length;
animEnemy.SetBool("die", false);
animEnemy.SetBool("bieg", true);
animEnemy.SetBool("atak", false);
Agent.SetDestination(enemyBaza); // porusza enemy w kierunku bazy
}
if (gracz == null) // gdy gracz spadnie lub go nie ma wyjdz z funkcji
{
gracz = GameObject.Find("PlayerController(Clone)"); // sprobuj pobrać gracza
enemyWraca = true; // enemy wraca do bazy
return;
} else enemyWraca = false;
dystans = Vector3.Distance(transform.position, gracz.transform.position);
if (healthEnemy.Death)
{
zdarzenieAudio = "die";
czasAudio = 3f;
//audioPlay("die", 1);
animEnemy.SetBool("die", true);
animEnemy.SetBool("bieg", false);
animEnemy.SetBool("atak", false);
Agent.SetDestination(transform.position);
//Debug.Log("oko");
} else
if (dystans < 84)
{
zdarzenieAudio = "bieg";
czasAudio = audioEnemyRun.length;
//AudioSource.PlayClipAtPoint(runEnemyAudio, transform.position);
Agent.SetDestination(gracz.transform.position); // porusza enemy w kierunku gracza
animEnemy.SetBool("bieg", true); // uruchamia animacje biegu
animEnemy.SetBool("atak", false); // uruchamia animacje ataku
animEnemy.SetBool("die", false);
if (dystans < 2)
{
zdarzenieAudio = "atak";
czasAudio = 2.37f;
//AudioSource.PlayClipAtPoint(attackEnemyAudio, transform.position);
graczTargetRotation = Quaternion.LookRotation(-gracz.transform.forward, Vector3.up);
// przekręć o 180 (korekta)
transform.rotation = Quaternion.Slerp(transform.rotation, graczTargetRotation, 10f *
Time.deltaTime); // obraca enemy w kierunku gracza
Agent.SetDestination(transform.position);
animEnemy.SetBool("bieg", false);
animEnemy.SetBool("atak", true);
animEnemy.SetBool("die", false);
}
//Debug.Log(dystans);
}
audioPlay(zdarzenieAudio, czasAudio);
if (photonView.isMine)
{
//
} else
{
transform.position = Vector3.Lerp(transform.position, realPosition, 0.1f);
transform.rotation = Quaternion.Lerp(transform.rotation , realRotation, 0.1f);
animEnemy.SetFloat( "AimAngle", Mathf.Lerp(animEnemy.GetFloat("AimAngle"),realAimAngle, 0.1f ));
}
}
public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
//CacheComponents();
if (stream.isWriting)
{
//Debug.Log("stream.isWriting!!");
}
if (animEnemy == null)
{
Debug.Log("enemyEmpty!!");
return;
}
if (stream.isWriting)
{
stream.SendNext(transform.position);
stream.SendNext(transform.rotation);
stream.SendNext( animEnemy.GetBool("bieg") );
stream.SendNext( animEnemy.GetBool("atak") );
stream.SendNext( animEnemy.GetBool("die") );
}else
{
//transform.position = stream.ReceiveNext() as Vector3; // ok - Old Shool ;)
//transform.position = (Vector3)stream.ReceiveNext(); // ok
realPosition = (Vector3)stream.ReceiveNext(); // ok
realRotation = (Quaternion)stream.ReceiveNext();
animEnemy.SetBool( "bieg", (bool)stream.ReceiveNext() );
animEnemy.SetBool( "atak", (bool)stream.ReceiveNext() );
animEnemy.SetBool( "die", (bool)stream.ReceiveNext() );
// realAimAngle = (float)stream.ReceiveNext();
if ( goFirstUpdate == false)
{
transform.position = realPosition;
transform.rotation = realRotation;
animEnemy.SetFloat("AimAngle", realAimAngle);
goFirstUpdate = true;
}
}
}
}
|