-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayerScript.cs
302 lines (241 loc) · 11 KB
/
PlayerScript.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
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;
public class PlayerScript : MonoBehaviour
{
[Header("Player Health Things")]
private float playerHealth = 1000f;
private float presentHealth;
public ProgressBar healthBar;
[Header("Player Movement")] //the header is used to indicate the variable use
public float playerSpeed = 1.9f;
public float currentPlayerSpeed = 0f;
public float playerSprint = 3f;
public float currentPlayerSprint = 0f;
[Header("Player Animator and Gravity")]
public CharacterController cC;
public float gravity = -9.81f;
public Animator animator;
[Header("Player Camera")]
public Transform playerCamera;
[Header("Player Jumping & velocity")]
public float turnCalmTime = 0.1f;
float turnCalmVelocity;
Vector3 velocity;
public Transform surfaceCheck; //to check if there is a surface below the player
bool onSurface; // to check if the player is on the surface
public float surfaceDistance = 0.4f;
public LayerMask surfaceMask;
public float jumpRange = 1f;
//MOBILE
public bool mobileInputs;
public FixedJoystick joystick;
public FixedJoystick Sprintjoystick;
private void Start()
{
//Cursor.lockState = CursorLockMode.Locked;
presentHealth = playerHealth;
healthBar.BarValue = 100; ;
}
// Update is called once per frame
void Update()
{
if (currentPlayerSpeed > 0)
{
Sprintjoystick = null;
}
else
{
FixedJoystick sprintJS = GameObject.Find("PlayerSprintJoystick").GetComponent<FixedJoystick>();
Sprintjoystick = sprintJS;
}
onSurface = Physics.CheckSphere(surfaceCheck.position, surfaceDistance, surfaceMask);
if (onSurface && velocity.y < 0)
{
velocity.y = -2f;//This ensures that the player is slightly pushed
//into the ground to prevent jittery behavior.
}
//gravity
velocity.y += gravity * Time.deltaTime;//when the player is on air , exp he is faaling we made ,
//the fall time dependant to keep it reel
cC.Move(velocity * Time.deltaTime);
playerMove();
Jump();
Sprint();
}
void playerMove()
{
if(mobileInputs == true) //if we're playing on mobile
{
float horizontalAxis =joystick.Horizontal;
float verticalAxis = joystick.Vertical;
// Move the player according to those inputs
Vector3 direction = new Vector3(horizontalAxis, 0f, verticalAxis).normalized; //converting it to a unit vector, which has a magnitude of 1
//nkhdmou 3ala vecteur unitaire dima
if (direction.magnitude >= 0.1f)
{
animator.SetBool("Walk", true);
animator.SetBool("Running", false); //when he's walking , no running
animator.SetBool("Idle", false);//when he's walking , no idle (we9if)
animator.SetTrigger("Jump"); //when walking, he can also jump
animator.SetBool("AimWalk", false);
animator.SetBool("IdleAim", false);
//tan(0) = x/y radian ->Deg
float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + playerCamera.eulerAngles.y;
//smoothing the rotation
float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnCalmVelocity, turnCalmTime);
//returns a rotation ->rotate our player making him face the direction of movement.
transform.rotation = Quaternion.Euler(0f, angle, 0f);
//calculate the moveDirection based on the rotation and the forward direction
Vector3 moveDirection = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
cC.Move(moveDirection.normalized * playerSpeed * Time.deltaTime);
currentPlayerSpeed = playerSpeed;
}
else //player not walking -> idle
{
animator.SetBool("Idle", true);
animator.SetTrigger("Jump");
animator.SetBool("Walk", false);
animator.SetBool("Running", false);
animator.SetBool("AimWalk", false);
currentPlayerSpeed = 0f;
}
}
else //if we're playing in unity
{
float horizontalAxis = Input.GetAxisRaw("Horizontal");
float verticalAxis = Input.GetAxisRaw("Vertical");
// Move the player according to those inputs
Vector3 direction = new Vector3(horizontalAxis, 0f, verticalAxis).normalized; //converting it to a unit vector, which has a magnitude of 1
//nkhdmou 3ala vecteur unitaire dima
if (direction.magnitude >= 0.1f)
{
animator.SetBool("Walk", true);
animator.SetBool("Running", false); //when he's walking , no running
animator.SetBool("Idle", false);//when he's walking , no idle (we9if)
animator.SetTrigger("Jump"); //when walking, he can also jump
animator.SetBool("AimWalk", false);
animator.SetBool("IdleAim", false);
//tan(0) = x/y radian ->Deg
float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + playerCamera.eulerAngles.y;
//smoothing the rotation
float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnCalmVelocity, turnCalmTime);
//returns a rotation ->rotate our player making him face the direction of movement.
transform.rotation = Quaternion.Euler(0f, angle, 0f);
//calculate the moveDirection based on the rotation and the forward direction
Vector3 moveDirection = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
cC.Move(moveDirection.normalized * playerSpeed * Time.deltaTime);
currentPlayerSpeed = playerSpeed;
}
else //player not walking -> idle
{
animator.SetBool("Idle", true);
animator.SetTrigger("Jump");
animator.SetBool("Walk", false);
animator.SetBool("Running", false);
animator.SetBool("AimWalk", false);
currentPlayerSpeed = 0f;
}
}
}
void Jump()
{
if (mobileInputs == true)
{
if (CrossPlatformInputManager.GetButtonDown("Jump") && onSurface)
{
animator.SetBool("Walk", false);
animator.SetTrigger("Jump");
velocity.y = Mathf.Sqrt(jumpRange * -2 * gravity);
}
else
{
animator.ResetTrigger("Jump");
}
}
else
{
if (Input.GetButtonDown("Jump") && onSurface) //GetButtonDown khtrna ninzlou 3al space to jump
{
animator.SetBool("Walk", false);
animator.SetTrigger("Jump");
velocity.y = Mathf.Sqrt(jumpRange * -2 * gravity); //vertical motion fomula=
//sqrt(height of the jump * gravity * 2
//hatina -2 khtr gravity = -9.8 so -2 * -9.8 >0
}
else
{
animator.ResetTrigger("Jump");
}
}
}
void Sprint()
{
if (mobileInputs == true) //if we're playing on mobile
{
float horizontalAxis = Sprintjoystick.Horizontal;
float verticalAxis = Sprintjoystick.Vertical;
Vector3 direction = new Vector3(horizontalAxis, 0f, verticalAxis).normalized;
if (direction.magnitude >= 0.1f)
{
animator.SetBool("Running", true);
animator.SetBool("Idle", false);
animator.SetBool("Walk", false);
animator.SetBool("IdleAim", false);
float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + playerCamera.eulerAngles.y;
float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnCalmVelocity, turnCalmTime);
transform.rotation = Quaternion.Euler(0f, angle, 0f);
Vector3 moveDirection = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
cC.Move(moveDirection.normalized * playerSprint * Time.deltaTime);
currentPlayerSprint = playerSprint;
}
else
{
animator.SetBool("Idle", false);
animator.SetBool("Walk", false);
currentPlayerSprint = 0f;
}
}
else //if we're playing in unity
{
float horizontalAxis = Input.GetAxisRaw("Horizontal");
float verticalAxis = Input.GetAxisRaw("Vertical");
Vector3 direction = new Vector3(horizontalAxis, 0f, verticalAxis).normalized;
if (direction.magnitude >= 0.1f)
{
animator.SetBool("Running", true);
animator.SetBool("Idle", false);
animator.SetBool("Walk", false);
animator.SetBool("IdleAim", false);
float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + playerCamera.eulerAngles.y;
float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnCalmVelocity, turnCalmTime);
transform.rotation = Quaternion.Euler(0f, angle, 0f);
Vector3 moveDirection = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
cC.Move(moveDirection.normalized * playerSprint * Time.deltaTime);
currentPlayerSprint = playerSprint;
}
else
{
animator.SetBool("Idle", false);
animator.SetBool("Walk", false);
currentPlayerSprint = 0f;
}
}
}
//playerHitDamage
public void playerHitDamage(float takeDamage)
{
presentHealth -= takeDamage;
healthBar.UpdateHealth(presentHealth, playerHealth); // Update the health bar
if (presentHealth <=0) {
PlayerDie();
}
}
private void PlayerDie()
{
Cursor.lockState = CursorLockMode.None;
Destroy(gameObject);
}
}