-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVelocityMovement
318 lines (224 loc) · 10.1 KB
/
VelocityMovement
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//Need to fix Jump Run
//When going down and jumping it gives you greater height
public class Velocity : MonoBehaviour
{
public CameraMovement cameraController;
//this is for the SphereCast it will create a sphere under the player to check if it touches anything
public LayerMask groundLayerMask;
//we will use this for the Raycast on walls to wall run
public LayerMask wallLayerMask;
//Checks if the player is in certain conditions
public bool isGrounded;
public bool isOnWall;
public bool isRunning;
public bool isWallRunningLeft;
public bool isWallRunningRight;
public bool tiltedLeft;
public bool tiltedRight;
//the speed and movement direction of the player
public float walkingSpeed = 5;
public float runningSpeed = 10;
public float wallRunningSpeed = 7.5f;
public float wallJumpPower;
private float moveNS;
private float moveEW;
public float wallGravityForce;
//takes the rigid component of an object
private Rigidbody rb;
//the force of the players jump
public float jumpForce = 6.5f;
public float airjumpForce = 5f;
//Can the player air jump?
public bool airJump;
//we save the directions the player move at so we can change their speed
Vector3 movedirection;
Vector3 wallJumpRight;
Vector3 wallJumpLeft;
Vector3 wallGravityPush;
Vector3 yFallVect;
//happens before the first frame
private void Awake()
{
//we take in the rigidbody compnent of the player
rb = GetComponent<Rigidbody>();
cameraController = GetComponentInChildren<CameraMovement>();
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
//the input value of the x and z axises
moveNS = Input.GetAxisRaw("Horizontal");
moveEW = Input.GetAxisRaw("Vertical");
//we now make a vector of where the player can move to and normalize it, which means it can go from 0 to 1
movedirection = (moveNS * transform.right + moveEW * transform.forward).normalized;
if (isGrounded || isWallRunningLeft || isWallRunningRight)
{
airJump = true;
}
if (transform.position.y < -2) // where -10 is the lowest the player can fall before reset
{
transform.position = Vector3.zero; // same as new Vector3(0, 0, 0);
GetComponent<Rigidbody>().velocity = Vector3.zero;
}
wallJumpRight = transform.right.normalized;
wallJumpLeft = -transform.right.normalized;
jump();
}
//Called every physics update
private void FixedUpdate()
{
yFallVect = new Vector3(0, rb.velocity.y, 0);
//checks whether the gameobject is grounded or not
isGrounded = Grounded();
isOnWall = againstWall();
Movement();
wallRun();
}
//In movement() and jump() I have not made it to the players movement are limited when jumping, may want to add it later
public void Movement()
{
//We save the previous y velocity of the player, we need this so the players y velocity doesn't always reset to 0 and stops physics acting weird
//to change how fast the player falls we need to go to edit -> Project Settings -> Gravity and change the "- value"
//Vector3 yFallVect = new Vector3(0, rb.velocity.y, 0);
//Checks whether the object is grounded and if no inputs are given, basically not moving
if ((isGrounded == true && !Input.GetButton("Vertical") && !Input.GetButton("Horizontal") && !Input.GetButtonDown("Jump")))
{
//if player does not move change the vector to 0
rb.velocity = yFallVect;
isRunning = false;
}
//Running input
else if (Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.LeftShift) && !Input.GetButtonDown("Jump") && isGrounded == true)
{
//the velocity of the players direction and resetting y to the players y previous value so it doesnt glitch
rb.velocity = movedirection * runningSpeed + yFallVect;
isRunning = true;
}
//Walking input
//Added not jumping so while we moving forward as the button first gets pressed down at least it can perform a jump
else if ((Input.GetButton("Vertical") || Input.GetButton("Horizontal")) && !Input.GetButtonDown("Jump") && isGrounded == true)
{
//Adds force relative to the rotation of object
rb.velocity = movedirection * walkingSpeed + yFallVect;
//player is not running anymore
isRunning = false;
}
}
private void jump()
{
Vector3 moveForce = new Vector3(moveNS, 0f, moveEW).normalized;
Vector3 currentXZVel = new Vector3(rb.velocity.x, 0f, rb.velocity.z);
//checks if player is grounded if so it can do part one of the jump
if ((isGrounded == true || isWallRunningLeft == true || isWallRunningRight == true) && Input.GetButtonDown("Jump"))
{
if (isWallRunningLeft == true)
{
rb.velocity = wallJumpRight * wallJumpPower + new Vector3(0f, airjumpForce, 0f) + currentXZVel;
}
else if (isWallRunningRight == true)
{
rb.velocity = wallJumpLeft * wallJumpPower + new Vector3(0f, airjumpForce, 0f) + currentXZVel;
}
//force of the jump
else if (isGrounded == true)
{
if (isRunning == true)
{
rb.velocity = movedirection * runningSpeed + new Vector3(0f, jumpForce, 0f);
}
else
{
rb.velocity = movedirection * walkingSpeed + new Vector3(0f, jumpForce, 0f);
}
}
}
//if no more grounded and still has an airjump the player can jump again
else if (!isWallRunningRight && !isWallRunningLeft && !isGrounded && airJump == true && Input.GetButtonDown("Jump"))
{
if (isRunning == true)
{
rb.velocity = movedirection * runningSpeed * 1.02f + new Vector3(0f, airjumpForce, 0f);
}
else
{
rb.velocity = movedirection * walkingSpeed * 1.02f + new Vector3(0f, airjumpForce, 0f);
}
airJump = false;
}
}
private void wallRun()
{
RaycastHit hit;
if (((isWallRunningLeft == true || isWallRunningRight == true) && isOnWall == false) || Input.GetKey(KeyCode.S) || (!Input.GetKey(KeyCode.W) && !Input.GetButton("Horizontal")) || ((Physics.Raycast(transform.position, -transform.right, out hit, 0.7f, wallLayerMask) && Input.GetKey(KeyCode.D) || Physics.Raycast(transform.position, transform.right, out hit, 0.7f, wallLayerMask) && Input.GetKey(KeyCode.A))))
{
rb.useGravity = true;
isWallRunningLeft = false;
isWallRunningRight = false;
if(tiltedLeft == true || tiltedRight == true)
{
cameraController.resetCameraAfterWallRun();
tiltedLeft = false;
tiltedRight = false;
}
}
else if (isOnWall == true && isGrounded == false && !Input.GetButton("Jump"))
{
rb.useGravity = false;
if (Physics.Raycast(transform.position, -transform.right, out hit, 0.7f, wallLayerMask))
{
if((Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.A)))
{
wallGravityPush = -hit.normal * wallGravityForce;
if(tiltedRight == false)
{
tiltedLeft = false;
cameraController.leftWallRunCamera();
tiltedRight = true;
}
isWallRunningLeft = true;
}
}
if (Physics.Raycast(transform.position, transform.right, out hit, 0.7f, wallLayerMask))
{
if ((Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.D)))
{
wallGravityPush = -hit.normal * wallGravityForce;
if (tiltedLeft == false)
{
tiltedRight = false;
cameraController.righttWallRunCamera();
tiltedLeft = true;
}
isWallRunningRight = true;
}
}
rb.velocity = movedirection * wallRunningSpeed + wallGravityPush;
}
}
private bool Grounded()
{
//Stores the value of what the raycast has hit
RaycastHit hit;
//if the return value is true then it returns true
//basically creates a sphrere to check beneath it
//transorm position takes in the position of set object
//GetComponentInChildren takes the component we ask for of any children
//0.55f and 0.49f are: one of them moves where the spheres is and the other sets the radius of the sphere outside of the hitbox which stops character from being grounded 1m high
//then vector3.down checks downwards
//and groundlayermask is the layer which is the floor
return Physics.SphereCast(transform.position + GetComponentInChildren<CapsuleCollider>().center, 0.55f, Vector3.down, out hit, 0.49f);
}
private bool againstWall()
{
RaycastHit hit;
//Different from spheres as we don need the radius anymore we only need the max distance, I used or because we need to check multiple sides to wallrun
return Physics.Raycast(transform.position, -transform.right, out hit, 0.7f, wallLayerMask) || Physics.Raycast(transform.position, transform.right, out hit, 0.7f, wallLayerMask);
}
}