-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayerMovement.cs
171 lines (154 loc) · 3.62 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
168
169
170
171
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.InputSystem;
[RequireComponent(typeof(Rigidbody2D))]
public class PlayerController : MonoBehaviour
{
public float walkSpeed = 5f;
public float runSpeed = 8f;
public float jumpForce = 0.1f;
Vector2 moveInput;
[SerializeField]
private bool _isRunning = false;
[SerializeField]
private bool _isMoving = false;
public bool _isFacingRight = true;
private bool isGrounded;
public Transform groundCheck;
public LayerMask groundLayer;
private bool hasJumped = false;
public bool IsFacingRight
{
get
{
return _isFacingRight;
}
private set
{
if(_isFacingRight !=value)
{
transform.Rotate(0f,180f,0f);
}
_isFacingRight =value;
}
}
public float CurrentMoveSpeed
{
get
{
if(IsMoving)
{
if(IsRunning)
{
return runSpeed;
}
else
{
return walkSpeed;
}
}
else
{
return 0;
}
}
}
public bool IsMoving
{
get
{
return _isMoving;
}
private set
{
_isMoving =value;
animator.SetBool("isMoving",value);
}
}
private bool IsRunning
{
get
{
return _isRunning;
}
set
{
_isRunning=value;
animator.SetBool("isRunning",value);
}
}
Rigidbody2D rb;
Animator animator;
private void Awake()
{
rb = GetComponent<Rigidbody2D>();
animator = GetComponent<Animator>();
}
void Start()
{
}
void Update()
{
if (!isGrounded && hasJumped)
{
hasJumped = false;
}
if (Input.GetButtonDown("Jump") && isGrounded && !hasJumped)
{
rb.velocity = new Vector2(rb.velocity.x, jumpForce);
hasJumped = true;
}
}
void FixedUpdate()
{
rb.velocity = new Vector2(moveInput.x * CurrentMoveSpeed * Time.fixedDeltaTime, rb.velocity.y);
// Check if the player is grounded
isGrounded = Physics2D.OverlapCircle(groundCheck.position, 0.1f, groundLayer);
}
public void OnMove(InputAction.CallbackContext context)
{
moveInput = context.ReadValue<Vector2>();
IsMoving = moveInput != Vector2.zero;
SetFacingDirection(moveInput);
}
private void SetFacingDirection(Vector2 moveInput)
{
if (moveInput.x > 0 && !IsFacingRight)
{
IsFacingRight = true;
}
if (moveInput.x < 0 && IsFacingRight)
{
IsFacingRight = false;
}
}
public void OnRun(InputAction.CallbackContext context)
{
if (context.started)
{
IsRunning = true;
}
else if (context.canceled)
{
IsRunning = false;
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Ground"))
{
hasJumped = false;
}
}
// New method for jumping
//doesnt work
public void OnJump(InputAction.CallbackContext context)
{
if (context.started && isGrounded)
{
Debug.Log("Jump input detected.");
rb.velocity = new Vector2(rb.velocity.x, jumpForce);
}
}
}