-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayerMovement.cs
167 lines (145 loc) · 4.74 KB
/
PlayerMovement.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
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour
{
//[SerializeField]
//LayerMask lmWalls;
[SerializeField]
float fJumpVelocity = 5;
float fJumpPressedRemember = 0;
[SerializeField]
float fJumpPressedRememberTime = 0.2f;
float fGroundedRemember = 0;
[SerializeField]
float fGroundedRememberTime = 0.25f;
//[SerializeField]
//float fHorizontalAcceleration = 1;
[SerializeField]
[Range(0, 1)]
float fHorizontalDampingBasic = 0.5f;
[SerializeField]
[Range(0, 1)]
float fHorizontalDampingWhenStopping = 0.5f;
[SerializeField]
[Range(0, 1)]
float fHorizontalDampingWhenTurning = 0.5f;
[SerializeField]
[Range(0, 1)]
float fCutJumpHeight = 0.5f;
public float dashAmount = 10f;
public bool facingRight = true;
private float moveHor;
public bool bGrounded;
private bool isPaused = false;
Rigidbody2D rb;
Animator anim;
AudioSource audioSource;
void Start()
{
rb = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
audioSource = GetComponent<AudioSource>();
}
// Update is called once per frame
void Update()
{
// Movement
PlayerMove();
Jump();
Dash();
if (Input.GetKeyDown(KeyCode.Escape) && !isPaused) {
Time.timeScale = 0;
print("hey");
isPaused = true;
}
else if (Input.GetKeyDown(KeyCode.Escape) && isPaused) {
Time.timeScale = 1;
print("hea");
isPaused = false;
}
}
void PlayerMove()
{
// Controls
moveHor = Input.GetAxis("Horizontal");
// Animation
if (moveHor != 0) {
anim.SetBool("isRunning", true);
}
else {
anim.SetBool("isRunning", false);
}
if (moveHor < 0.0f && facingRight == true) {
FlipPlayer();
}
else if (moveHor > 0.0f && facingRight == false) {
FlipPlayer();
}
if (anim.GetBool("isRunning") == true && bGrounded){
if (!audioSource.isPlaying){
audioSource.Play();
}
}
else {
audioSource.Stop();
}
// Physics
float fHorizontalVelocity = rb.velocity.x;
fHorizontalVelocity += Input.GetAxisRaw("Horizontal");
if (Mathf.Abs(Input.GetAxisRaw("Horizontal")) < 0.01f)
fHorizontalVelocity *= Mathf.Pow(1f - fHorizontalDampingWhenStopping, Time.deltaTime * 10f);
else if (Mathf.Sign(Input.GetAxisRaw("Horizontal")) != Mathf.Sign(fHorizontalVelocity))
fHorizontalVelocity *= Mathf.Pow(1f - fHorizontalDampingWhenTurning, Time.deltaTime * 10f);
else
fHorizontalVelocity *= Mathf.Pow(1f - fHorizontalDampingBasic, Time.deltaTime * 10f);
rb.velocity = new Vector2(fHorizontalVelocity, rb.velocity.y);
}
void Jump()
{
//Vector2 v2GroundedBoxCheckPosition = (Vector2)transform.position + new Vector2(0, -0.01f);
//Vector2 v2GroundedBoxCheckScale = (Vector2)transform.localScale + new Vector2(-0.02f, 0);
//bool bGrounded = Physics2D.OverlapBox(v2GroundedBoxCheckPosition, v2GroundedBoxCheckScale, 0, lmWalls);
fGroundedRemember -= Time.deltaTime;
if (bGrounded) {
fGroundedRemember = fGroundedRememberTime;
}
fJumpPressedRemember -= Time.deltaTime;
if (Input.GetButtonDown("Jump")) {
fJumpPressedRemember = fJumpPressedRememberTime;
}
if (Input.GetButtonUp("Jump")) {
if (rb.velocity.y > 0) {
rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y * fCutJumpHeight);
}
}
if ((fJumpPressedRemember > 0) && (fGroundedRemember > 0)) {
fJumpPressedRemember = 0;
fGroundedRemember = 0;
rb.velocity = new Vector2(rb.velocity.x, fJumpVelocity);
bGrounded = false;
FindObjectOfType<AudioManager>().Play("Jump");
}
}
void FlipPlayer()
{
facingRight = !facingRight;
Vector2 localScale = gameObject.transform.localScale;
localScale.x *= -1;
transform.localScale = localScale;
}
void Dash()
{
if (Input.GetKeyDown(KeyCode.Space)){
if (facingRight)
rb.MovePosition(transform.position + new Vector3(dashAmount, 0));
else
rb.MovePosition(transform.position - new Vector3(dashAmount, 0));
}
}
void OnCollisionEnter2D(Collision2D collision)
{
if (collision.collider.tag == "Ground" || collision.collider.tag == "Enemy") {
bGrounded = true;
}
}
}