-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVelocityMess
250 lines (188 loc) · 8.05 KB
/
VelocityMess
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//Need to fix Jump Run
//need to fix 2nd jump
public class Velocity : MonoBehaviour
{
//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 onWall;
public bool isJumping;
public bool isRunning;
public bool isWallRunning;
//the speed and movement direction of the player
public float walkingSpeed = 5;
public float runningSpeed = 10;
public float wallRunningSpeed = 7.5f;
private float moveNS;
private float moveEW;
//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;
//happens before the first frame
private void Awake()
{
//we take in the rigidbody compnent of the player
rb = GetComponent<Rigidbody>();
}
// 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)
{
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;
}
//is jumping
jump();
}
//Called every physics update
private void FixedUpdate()
{
//checks whether the gameobject is grounded or not
IsGrounded = Grounded();
onWall = againstWall();
//activates movement
Movement();
}
//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 ( isJumping == false && !Input.GetButton("Vertical") && !Input.GetButton("Horizontal") && !Input.GetButton("Jump"))
{
//if player does not move change the vector to 0
//rb.velocity = Vector3.zero + yFallVect;
isRunning = false;
}
//Running input
else if (Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.LeftShift) && !Input.GetButtonDown("Jump"))
{
//wall Run
if (onWall == true && IsGrounded == false)
{
rb.velocity = movedirection * wallRunningSpeed;
rb.useGravity = false;
isWallRunning = true;
}
//running
else if (isJumping == false)
{
rb.useGravity = 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") && isJumping == false)
{
//Adds force relative to the rotation of object
rb.velocity = movedirection * walkingSpeed + yFallVect;
rb.useGravity = true;
//player is not running anymore
isRunning = false;
}
if (!onWall && isWallRunning == true)
{
isWallRunning = false;
}
}
private void jump()
{
//checks if player is grounded if so it can do part one of the jump
if (IsGrounded == true && Input.GetButtonDown("Jump"))
{
//force of the jump
//rb.AddForce(0f, jumpForce, 0f, ForceMode.Acceleration);
rb.velocity = new Vector3(0f, jumpForce, 0f);
}
//if no more grounded and still has an airjump the player can jump again
else if (!IsGrounded && airJump == true && Input.GetButtonDown("Jump"))
{
//force of the jump
if (rb.velocity.y < 0f)
{
//can use both force or velocity but velocity goes against gravity {Kind of resets its downward force}
rb.velocity = new Vector3(0f, airjumpForce, 0f);
//rb.AddForce(0f, jumpForce ,0f);
airJump = false;
}
else
{
rb.velocity = rb.velocity + new Vector3(0f, airjumpForce, 0f);
//rb.AddForce(0f, jumpForce, 0f);
airJump = false;
}
}
//will check if the player is still on the ground or not
if ((IsGrounded == true || isWallRunning == true) && isJumping == true)
{
isJumping = false;
}
else if ((!IsGrounded && !isWallRunning) && isJumping == false )
{
isJumping = true;
}
//if the player is run jumping then we do this
if (isJumping == true && isRunning == true)
{
Vector3 yFallVect = new Vector3(0, rb.velocity.y, 0);
//the velocity of the players direction and resetting y to the players y previous value so it doesnt glitch
rb.velocity = movedirection * runningSpeed + yFallVect;
}
//if player is only walk jumping then do this
else if (isJumping == true && isRunning == false)
{
Vector3 yFallVect = new Vector3(0, rb.velocity.y, 0);
rb.velocity = movedirection * walkingSpeed + yFallVect;
}
}
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, groundLayerMask);
}
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.TransformDirection(Vector3.left), out hit, 0.6f, wallLayerMask) || Physics.Raycast(transform.position, transform.TransformDirection(Vector3.right), out hit, 0.6f, wallLayerMask);
}
}