2026-01-30 06:16:16 +00:00
|
|
|
using UnityEngine;
|
|
|
|
|
using UnityEngine.InputSystem;
|
|
|
|
|
|
|
|
|
|
public class PlayerMovement : MonoBehaviour
|
|
|
|
|
{
|
|
|
|
|
private Rigidbody2D _rigidbody2D;
|
2026-01-30 07:02:37 +00:00
|
|
|
private Collider2D _collider2D;
|
2026-01-30 06:16:16 +00:00
|
|
|
|
2026-01-30 07:02:37 +00:00
|
|
|
[Header("Settings")]
|
2026-01-30 06:16:16 +00:00
|
|
|
[SerializeField] private float moveSpeed = 5f;
|
|
|
|
|
[SerializeField] private float jumpForce = 10f;
|
|
|
|
|
|
2026-01-30 07:02:37 +00:00
|
|
|
[Header("Detection")]
|
2026-01-30 06:16:16 +00:00
|
|
|
[SerializeField] private BoxCollider2D groundCheckCollider;
|
2026-01-30 07:02:37 +00:00
|
|
|
[SerializeField] private BoxCollider2D hangWallCheckCollider;
|
2026-01-30 06:16:16 +00:00
|
|
|
[SerializeField] private LayerMask groundLayer;
|
2026-01-30 07:02:37 +00:00
|
|
|
|
2026-01-30 06:16:16 +00:00
|
|
|
private Vector2 inputVector;
|
2026-01-30 07:02:37 +00:00
|
|
|
private bool isHanging = false;
|
2026-01-30 06:16:16 +00:00
|
|
|
|
|
|
|
|
private void Awake()
|
|
|
|
|
{
|
2026-01-30 07:02:37 +00:00
|
|
|
_rigidbody2D = GetComponent<Rigidbody2D>();
|
2026-01-30 10:53:09 +00:00
|
|
|
_collider2D = GetComponent<BoxCollider2D>();
|
2026-01-30 06:16:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void FixedUpdate()
|
|
|
|
|
{
|
2026-01-30 07:02:37 +00:00
|
|
|
if (isHanging)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
HandleRotation();
|
2026-01-30 06:16:16 +00:00
|
|
|
_rigidbody2D.linearVelocity = new Vector2(inputVector.x * moveSpeed, _rigidbody2D.linearVelocity.y);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnMove(InputValue value)
|
|
|
|
|
{
|
|
|
|
|
inputVector = value.Get<Vector2>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnJump(InputValue value)
|
|
|
|
|
{
|
2026-01-30 07:02:37 +00:00
|
|
|
if (value.isPressed)
|
2026-01-30 06:16:16 +00:00
|
|
|
{
|
2026-01-30 07:02:37 +00:00
|
|
|
if (isHanging)
|
|
|
|
|
{
|
|
|
|
|
CancelHanging();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
else if (IsGrounded())
|
|
|
|
|
{
|
|
|
|
|
_rigidbody2D.linearVelocity = new Vector2(_rigidbody2D.linearVelocity.x, jumpForce);
|
|
|
|
|
}
|
2026-01-30 06:16:16 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-30 07:02:37 +00:00
|
|
|
private void OnHang(InputValue value)
|
|
|
|
|
{
|
|
|
|
|
if (value.isPressed)
|
|
|
|
|
{
|
|
|
|
|
if (isHanging)
|
|
|
|
|
{
|
|
|
|
|
CancelHanging();
|
|
|
|
|
}
|
|
|
|
|
else if (hangWallCheckCollider.IsTouchingLayers(groundLayer))
|
|
|
|
|
{
|
|
|
|
|
HangingObject();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void CancelHanging()
|
|
|
|
|
{
|
|
|
|
|
isHanging = false;
|
|
|
|
|
_rigidbody2D.bodyType = RigidbodyType2D.Dynamic;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void HangingObject()
|
|
|
|
|
{
|
|
|
|
|
isHanging = true;
|
|
|
|
|
_rigidbody2D.bodyType = RigidbodyType2D.Kinematic;
|
|
|
|
|
_rigidbody2D.linearVelocity = Vector2.zero;
|
|
|
|
|
SnapToWall();
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-30 06:16:16 +00:00
|
|
|
private bool IsGrounded()
|
|
|
|
|
{
|
2026-01-30 07:02:37 +00:00
|
|
|
return groundCheckCollider.IsTouchingLayers(groundLayer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void HandleRotation()
|
|
|
|
|
{
|
|
|
|
|
if (inputVector.x > 0)
|
|
|
|
|
{
|
|
|
|
|
transform.rotation = Quaternion.Euler(0, 0, 0);
|
|
|
|
|
}
|
|
|
|
|
else if (inputVector.x < 0)
|
|
|
|
|
{
|
|
|
|
|
transform.rotation = Quaternion.Euler(0, 180, 0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SnapToWall()
|
|
|
|
|
{
|
|
|
|
|
float direction = transform.rotation.eulerAngles.y == 180 ? -1f : 1f;
|
|
|
|
|
RaycastHit2D hit = Physics2D.Raycast(transform.position, Vector2.right * direction, 1.0f, groundLayer);
|
|
|
|
|
|
|
|
|
|
if (hit.collider != null)
|
|
|
|
|
{
|
|
|
|
|
float playerHalfWidth = _collider2D.bounds.extents.x;
|
|
|
|
|
float newX = hit.point.x - (direction * playerHalfWidth);
|
|
|
|
|
|
|
|
|
|
transform.position = new Vector2(newX, transform.position.y);
|
|
|
|
|
}
|
2026-01-30 06:16:16 +00:00
|
|
|
}
|
2026-01-30 07:02:37 +00:00
|
|
|
}
|