-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayerController.cs
More file actions
226 lines (190 loc) · 5.65 KB
/
PlayerController.cs
File metadata and controls
226 lines (190 loc) · 5.65 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
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
//Data
public float speed;
public float jumpForce;
private float moveInput;
private float moveVerticalInput;
public int extraJumps;
public int totalExtraJumps;
public float climbSpeed;
public float launchForce;
private float originalGravityScale;
//Objects
public GameObject SplashParticles;
//Is variables
private bool isOnWater;
public bool isIdle;
private bool isClimbing;
public bool isFalling;
public bool isJumping;
private bool facingRight = true;
//Components
public Rigidbody2D rb;
public SpriteRenderer spriteRenderer;
public Animator playerAnimator;
//Ground Detection
public bool isGrouded;
public Transform isGroudedDetector;
public float checkRadius;
public LayerMask whatIsGround;
void Start()
{
//Set permanent data
originalGravityScale = rb.gravityScale;
extraJumps = totalExtraJumps;
}
void Update()
{
if (FindObjectOfType<GameManager>().isDead == true)
{
FindObjectOfType<PlayerController>().enabled = false;
}
//Is modifications-
if (rb.velocity.x < 0.05f && rb.velocity.y < 0.05f && rb.velocity.x > -0.05f && rb.velocity.y > -0.05f)
{
isIdle = true;
}
else
{
isIdle = false;
}
if (rb.velocity.y > 0.05 && isGrouded == false && isClimbing == false)
{
isJumping = true;
isFalling = false;
}
else if (rb.velocity.y < -0.05 && isGrouded == false && isClimbing == false)
{
isFalling = true;
isJumping = false;
}
else
{
isJumping = false;
isFalling = false;
}
if (isGrouded == true)
{
extraJumps = totalExtraJumps;
}
//-Is Modifications
//Flip-
if (facingRight == false && moveInput > 0)
{
facingRight = !facingRight;
spriteRenderer.flipX = false;
}
else if (facingRight == true && moveInput < 0)
{
facingRight = !facingRight;
spriteRenderer.flipX = true;
}
//-Flip
//Jump-
if (Input.GetKeyDown(KeyCode.W) && extraJumps > 0 && isClimbing == false)
{
rb.velocity = Vector2.up * jumpForce;
extraJumps--;
}
else if (Input.GetKeyDown(KeyCode.W) && extraJumps == 0 && isGrouded == true && isClimbing == false)
{
rb.velocity = Vector2.up * jumpForce;
}
//-Jump
}
void FixedUpdate()
{
//Ground Verification-
isGrouded = Physics2D.OverlapCircle(isGroudedDetector.position, checkRadius, whatIsGround);
//-Ground Verification
//Horizontal movement-
moveInput = Input.GetAxisRaw("Horizontal");
rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
//-Horizontal movement
//Climp-
if (isClimbing == true)
{
moveVerticalInput = Input.GetAxisRaw("Vertical");
rb.velocity = new Vector2(rb.velocity.x, moveVerticalInput * climbSpeed);
rb.gravityScale = 0f;
}
//-Climp
}
private void LateUpdate()
{
//Animator with code-
if (isIdle == true && isClimbing == false && isGrouded == true)
{
playerAnimator.Play("Idle");
}
else if (isClimbing == true)
{
if (isIdle == true)
{
playerAnimator.Play("Scale_Idle");
}
else
{
playerAnimator.Play("Scale");
}
}
else if (isGrouded == true && isClimbing == false && isIdle == false || isOnWater == true)
{
playerAnimator.Play("Walk");
}
else if (isJumping == true && isFalling == false && isOnWater == false)
{
playerAnimator.Play("Jump_Up");
}
else if (isFalling == true && isJumping == false && isOnWater == false)
{
playerAnimator.Play("Jump_Down");
}
//-Animator with code
}
private void OnTriggerEnter2D(Collider2D collision)
{
//Activate splash particles
if (collision.CompareTag("Agua") && rb.velocity.y < -10)
{
SplashParticles.SetActive(true);
Invoke("DesactivateParticles", 1f);
}
}
private void OnTriggerStay2D(Collider2D collision)
{
//Know where can climp
if (collision.CompareTag("Ladder"))
{
isClimbing = true;
}
//Know where can swim
if (collision.CompareTag("Agua"))
{
isOnWater = true;
}
}
private void OnTriggerExit2D(Collider2D collsion)
{
//Reset some variables
isClimbing = false;
rb.gravityScale = originalGravityScale;
isOnWater = false;
}
void OnCollisionEnter2D(Collision2D other)
{
//Know when the player touch a trampoline
if (other.gameObject.CompareTag("Trampoline"))
{
rb.velocity = Vector2.up * launchForce;
}
}
void DesactivateParticles()
{
SplashParticles.SetActive(false);
}
}