-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayerMovement.cs
More file actions
191 lines (162 loc) · 5.46 KB
/
PlayerMovement.cs
File metadata and controls
191 lines (162 loc) · 5.46 KB
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Assets.Scripts.Weapon;
public class PlayerMovement : MonoBehaviour
{
public CharacterController controller;
public WeaponManger weaponManager;
public PlayerUI playerUI;
public Camera camera;
public float walkingSpeed = 8f;
public float runningSpeed = 15f;
public float crouchSpeed = 2f;
public float gravity = -9.81f;
public float jumpHeight = 3f;
public float crouchHeight = 2f;
private float speed = 0;
private Vector3 velocity;
private bool isGrounded;
private bool isWalking = true;
private bool isCrouching = false;
private float extraJump = 1.0f;
private float forward = 0f;
private float side = 0f;
public delegate void SetBool(bool active);
public delegate void WeaponPosition(string pos, bool state);
public event SetBool Headbobing;
public event WeaponPosition WeaponPositionEvent;
// Start is called before the first frame update
void Start() {
speed = walkingSpeed;
}
// Update is called once per frame
void Update()
{
isGrounded = IsGrounded();
if (isGrounded)
{
//WeaponPositionEvent(GunAnimation.Hide, false);
Headbobing(true);
}
else
{
//WeaponPositionEvent(GunAnimation.Hide, true);
Headbobing(false);
}
if (isGrounded && velocity.y < 0)
velocity.y = -2f;
HandleCrouch();
if (isGrounded && !isCrouching)
controller.height = 3.8f;
HandleJump();
side = Input.GetAxis("Horizontal");
forward = Input.GetAxis("Vertical");
// avoids moving to the sides while jumping
if (!isGrounded && !isCrouching)
side = 0;
// if an object blocks the weapon
bool isWeaponBlocked = Physics.Raycast(transform.position, Vector3.forward, 1f);
// vector of the move direction
Vector3 moveDirection = Vector3.Normalize(transform.right * side + transform.forward * forward);
if (true)//!isWeaponBlocked)
{
HandleRun();
controller.Move(moveDirection * speed * Time.deltaTime);
}
// sets gravity on y axis
velocity.y += gravity * Time.deltaTime;
// applies garvity
controller.Move(velocity * Time.deltaTime);
}
private void HandleJump()
{
if (Input.GetButtonDown("Jump"))//&& isGrounded)
{
if (isGrounded || (isGrounded && extraJump >= 3f))
extraJump = 1.0f;
else
extraJump++;
if (extraJump < 3f)
{
if (extraJump > 1f)
playerUI.EffectEnergy(-10);
// required velocity for jump is root(h*-2*g)
velocity.y = Mathf.Sqrt(jumpHeight * extraJump * -2f * gravity);
//controller.height = 2.5f;
}
}
}
private void HandleRun()
{
if (Input.GetKey(KeyCode.LeftShift) && !isCrouching && !weaponManager.Weapon.Reloading)
{
if (forward > 0)
{
if (isGrounded)
WeaponPositionEvent(WeaponAnimations.Run, true);
else
WeaponPositionEvent(WeaponAnimations.Run, false);
speed = runningSpeed;
isWalking = false;
}
}
else
{
WeaponPositionEvent(WeaponAnimations.Run, false);
isWalking = true;
if (!isCrouching)
speed = walkingSpeed;
}
}
private void HandleCrouch()
{
if (Input.GetKeyDown(KeyCode.C) && isGrounded)
{
// checks if player is blocked by objects from above
bool blockedFromTop = Physics.Raycast(transform.position, Vector3.up, 2f);
// exits the method if player is blocked and already crouching, won't be able to stand up back anyway
if (blockedFromTop && isCrouching)
return;
isCrouching = !isCrouching;
// scale of the y axis while crouching/standing
float crouchScale = 0;
float verticalPosFix = 0;
if (isCrouching)
{
controller.height = crouchHeight;
// decreases player's size
crouchScale = transform.localScale.y / 1.5f;
verticalPosFix = crouchHeight;
speed = crouchSpeed;
}
else
{
controller.height = 3.8f;
verticalPosFix = -crouchHeight;
// increases back player's size
crouchScale = transform.localScale.y * 1.5f;
}
// set size of player
//transform.localScale = new Vector3(transform.localScale.x, crouchScale, transform.localScale.z);
// fixing position
transform.position = new Vector3(transform.position.x, transform.position.y + crouchHeight, transform.position.z);
}
}
private bool IsGrounded()
{
return Physics.Raycast(transform.position, -transform.up, controller.height/2 + 0.4f);
}
public bool OnGround
{
get { return isGrounded; }
}
public bool CanFire
{
get { return isWalking || isCrouching; }
}
public bool IsWalking
{
get { return isWalking; }
}
}