This repository was archived by the owner on Jan 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayerController2D.cs
159 lines (130 loc) · 4.38 KB
/
PlayerController2D.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
using UnityEngine;
public class PlayerController2D : MonoBehaviour
{
// Variables
public float Speed;
public ParticleSystem rightDust;
public ParticleSystem leftDust;
private Animator animator;
private Rigidbody2D rb;
private CapsuleCollider2D capsule;
private AudioSource audioSource;
private float moveInput;
private bool FacingRight;
private bool isMoving;
// Jumping Variables
public int extraJumps;
public float radius;
public float jumpforce;
public LayerMask whatisGround;
public BoxCollider2D box;
private int extraJumpValue;
public float vertical;
private void Start()
{
// Gathering components
rb = GetComponent<Rigidbody2D>();
animator = GetComponent<Animator>();
box = GetComponent<BoxCollider2D>();
capsule = GetComponent<CapsuleCollider2D>();
audioSource = GetComponent<AudioSource>();
// Setting the starting values
FacingRight = true;
extraJumpValue = extraJumps;
}
private void FixedUpdate()
{
// Movement
moveInput = Input.GetAxis("Horizontal");
animator.SetFloat("Speed", Mathf.Abs(moveInput));
rb.velocity = new Vector2(moveInput * Speed, rb.velocity.y);
if (FacingRight == true && moveInput < 0){
Flip();
}
else if (FacingRight == false && moveInput > 0){
Flip();
}
// End of movement
// isMoving Check
if (rb.velocity.x != 0){
isMoving = true;
}else{
isMoving = false;
}
// Setting the audio
if (isMoving && isGrounded()){
if (!audioSource.isPlaying){
audioSource.Play();
}
}else{
audioSource.Stop();
}
// Setting Dust mechanic (Particles)
if (FacingRight && isMoving && isGrounded()){
StopLeft();
RightDust();
}else if (!FacingRight && isMoving && isGrounded() ){
StopRight();
LeftDust();
}else if (!isMoving){
StopRight();
StopLeft();
}
}
void Update()
{
// Jumping
vertical = rb.velocity.y;
if (isGrounded()){
vertical = 0;
animator.SetBool("isJumping", false);
extraJumpValue = extraJumps;
}
animator.SetFloat("Vertical", vertical);
animator.SetBool("isJumping", !isGrounded());
if (Input.GetKeyDown(KeyCode.W) && extraJumpValue > 0){
Soundmanager.PlaySound("Jump");
rb.velocity = Vector2.up * jumpforce;
extraJumpValue -= 1;
}
}
// Functions
void StopRight(){
rightDust.Stop();
}
void StopLeft(){
leftDust.Stop();
}
void RightDust(){
rightDust.Play();
}
void LeftDust(){
leftDust.Play();
}
void Flip(){
FacingRight = !FacingRight;
transform.Rotate(0f, 180f, 0f);
}
// isGrounded function
private bool isGrounded(){
// --- Box Collider Check w/Raycast ---
float extraHeight = 0.5f;
RaycastHit2D raycastHit = Physics2D.BoxCast(box.bounds.center, box.bounds.size, 0f, Vector2.down, extraHeight, whatisGround);
// RaycastHit2D tryhit = Physics2D.CapsuleCast(capsule.bounds.center, capsule.bounds.size, "Vertical", 0f, Vector2.down, extraHeight, whatisGround);
// --- Raycast Coloring ---
Color raycastColor;
if (raycastHit.collider != null)
{
raycastColor = Color.green;
}
else
{
raycastColor = Color.red;
}
// --- Draw Gizmos ---
Debug.DrawRay(box.bounds.center + new Vector3(box.bounds.extents.x, 0), Vector2.down * (box.bounds.extents.y + extraHeight), raycastColor);
Debug.DrawRay(box.bounds.center - new Vector3(box.bounds.extents.x, 0), Vector2.down * (box.bounds.extents.y + extraHeight), raycastColor);
Debug.DrawRay(box.bounds.center - new Vector3(box.bounds.extents.x, box.bounds.extents.y), Vector2.right * (box.bounds.extents.x), raycastColor);
return raycastHit.collider != null;
}
}