Repository navigation
Expand file tree
/
Copy pathAirConsoleController.cs
More file actions
147 lines (120 loc) · 3.69 KB
/
Copy pathAirConsoleController.cs
File metadata and controls
147 lines (120 loc) · 3.69 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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AirConsoleController : MonoBehaviour
{
Animator animator;
Rigidbody2D rgby;
SpriteRenderer spriteRenderer;
bool isGrounded;
bool movingRight;
bool movingLeft;
bool jump;
[SerializeField]
Transform groundCheck;
[SerializeField]
private LayerMask m_WhatIsGround;
[SerializeField]
private float runSpeed = 20f;
private float jumpForce = 20f;
private float horizontalMove = 0f;
// Start is called before the first frame update
void Start()
{
animator = GetComponent<Animator>();
rgby = GetComponent<Rigidbody2D>();
spriteRenderer = GetComponent<SpriteRenderer>();
DontDestroyOnLoad(this);
}
public void ButtonInput(string input)
{
switch (input)
{
case "right":
movingRight = true;
movingLeft = false;
break;
case "left":
movingLeft = true;
movingRight = false;
break;
case "right-up":
movingRight = false;
movingLeft = false;
jump = false;
break;
case "left-up":
movingLeft = false;
movingRight = false;
jump = false;
break;
case "jump":
jump = true;
break;
case "jump-up":
jump = false;
break;
}
}
// Update is called once per frame
private void FixedUpdate()
{
isGrounded = false;
animator.SetFloat("Speed", horizontalMove);
Collider2D[] colliders = Physics2D.OverlapCircleAll(groundCheck.position, 0.2f, m_WhatIsGround);
for (int i = 0; i < colliders.Length; i++)
{
if (colliders[i].gameObject != gameObject)
{
isGrounded = true;
}
}
if(movingRight && !movingLeft)
{
runSpeed = 8f;
rgby.velocity = new Vector2(runSpeed, rgby.velocity.y);
if (isGrounded) {
horizontalMove = 10f;
animator.SetBool("IsJumping", false);
}
spriteRenderer.flipX = false;
}
else if (movingLeft && !movingRight)
{
runSpeed = 8f;
rgby.velocity = new Vector2(-runSpeed, rgby.velocity.y);
if (isGrounded) {
horizontalMove = 10f;
animator.SetBool("IsJumping", false);
}
spriteRenderer.flipX = true;
}
else
{
if (isGrounded)
animator.SetBool("IsJumping", false);
rgby.velocity = new Vector2(0, rgby.velocity.y);
}
if(jump && isGrounded)
{
rgby.velocity = new Vector2(rgby.velocity.x, jumpForce);
animator.SetBool("IsJumping", true);
isGrounded = false;
}
if(!movingLeft && !movingRight)
{
horizontalMove = 0f;
runSpeed = 0f;
}
}
void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag.Equals("Moving Platform"))
this.transform.parent = collision.transform;
}
void OnCollisionExit2D(Collision2D collision)
{
if (collision.gameObject.tag.Equals("Moving Platform"))
this.transform.parent = null;
}
}