-
Notifications
You must be signed in to change notification settings - Fork 0
/
Movement.cs
147 lines (134 loc) · 4.92 KB
/
Movement.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour
{
#region classMembers
Animator anim;
Rigidbody2D rb;
[SerializeField] Collider2D standingCollider;
[SerializeField] Transform groundCheckCollider;
[SerializeField] Transform overheadCheckCollider;
[SerializeField] LayerMask groundLayer;
[SerializeField] float jumpPower;
public float movementModifier;
public float runModifier = 2f;
public float crouchSpeedModifier = 0.5f;
float hVal;
const float groundCheckRadius = 0.2f;
const float overheadCheckRadius = 0.2f;
bool facingRight = true;
bool isRunning = false;
bool isGrounded = false;
bool crouchPressed;
#endregion
void Awake()
{
//Init player RB2D at game start
rb = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
}
void Update()
{
//Poll for horizontal input at each frame
hVal = Input.GetAxisRaw("Horizontal");
//Poll for Lshift key for running
if (Input.GetKeyDown(KeyCode.LeftShift))
isRunning = true;
if (Input.GetKeyUp(KeyCode.LeftShift))
isRunning = false;
#region Jump and Crouch Key
//Jump
if (Input.GetButtonDown("Jump"))
Jump();
//Crouch
if (Input.GetButtonDown("Crouch"))
crouchPressed = true;
else if (Input.GetButtonUp("Crouch"))
crouchPressed = false;
//Set yvelocity in animator
anim.SetFloat("yVelocity", rb.velocity.y);
#endregion
}
void FixedUpdate()
{
//Check if player is standing on ground layer
GroundCheck();
//Call Jump function
//Call move function with axis input each update
Move(hVal, crouchPressed);
}
void GroundCheck()
{
isGrounded = false;
//Check if player groundcheck object is colliding with ground colliders
//If yes: isGrounded = true else:isGrounded = false;
Collider2D[] colliders = Physics2D.OverlapCircleAll(groundCheckCollider.position, groundCheckRadius, groundLayer);
if (colliders.Length > 0)
{
isGrounded = true;
}
//As long as isGrounded=true. jump animation bool is disabled
anim.SetBool("Jump", !isGrounded);
}
void Jump()
{
if (isGrounded)
{
//OLD-> rb.AddForce(new Vector2(0f, jumpPower));
rb.velocity = Vector2.up * jumpPower;
anim.SetBool("Jump", true);
}
}
void Move(float dir, bool crouchFlag)
{
#region crouch
//If we are crouching and disabled crouching
//Check for overhead collision with ground
//If check returns true, remain crouched
//Otherwise cancel crouch
if (!crouchFlag)
{
if (Physics2D.OverlapCircle(overheadCheckCollider.position, overheadCheckRadius, groundLayer))
crouchFlag = true;
}
//If we hit crouch key, disable standingCollider while crouched + animate crouch
//Reduce the speed while crouched to half walking speed (3)
//if released, resume original speed and enable standing collider, disable crouch animation
//Set Animation
anim.SetBool("isCrouching", crouchFlag);
standingCollider.enabled = !crouchFlag;
//If player is grounded and presses space -> jump
#endregion
#region move&run
//Current scale data.
Vector3 currentScale = transform.localScale;
//Base value of x axis velocity. Dictated by direction of axis and movement modifier float
float xvalue = dir * movementModifier * Time.fixedDeltaTime;
//If running, increase speed by runModifier
if (isRunning)
xvalue = dir * movementModifier * runModifier * Time.fixedDeltaTime;
//If crouched, reduce speed by crouchSpeedModifier
if (crouchFlag)
xvalue = dir * movementModifier * crouchSpeedModifier * Time.fixedDeltaTime;
//Final Run speed
rb.velocity = new Vector2(xvalue, rb.velocity.y);
//If player is facing right and axis is negative, flip left
if (facingRight && dir < 0)
{
transform.localScale = new Vector3(transform.localScale.x * -1, transform.localScale.y, transform.localScale.z);
facingRight = false;
}
//If player is facing left and axis is positive, flip right
else if (!facingRight && dir > 0)
{
transform.localScale = new Vector3((transform.localScale.x * -1), transform.localScale.y, transform.localScale.z);
facingRight = true;
}
currentScale = transform.localScale;
//0 idle,3 crouched, 6 walking, 12 running
//Set xvelocity of anim to abs value of x velocity
anim.SetFloat("xVelocity", Mathf.Abs(rb.velocity.x));
#endregion
}
}