68 lines
1.9 KiB
C#
68 lines
1.9 KiB
C#
using UnityEngine;
|
|
|
|
public class PlayerController : MonoBehaviour
|
|
{
|
|
[SerializeField]
|
|
private Animator animator;
|
|
[SerializeField]
|
|
private float MovementSpeed = 1.5f;
|
|
[SerializeField]
|
|
private LayerMask ladderLayer;
|
|
[SerializeField]
|
|
private LayerMask groundLayer;
|
|
[SerializeField]
|
|
private float distance;
|
|
private bool _isOnLadder;
|
|
private Rigidbody2D _body;
|
|
private CapsuleCollider2D _capsule;
|
|
|
|
void Start()
|
|
{
|
|
_body = GetComponent<Rigidbody2D>();
|
|
_capsule= GetComponent<CapsuleCollider2D>();
|
|
}
|
|
void Update()
|
|
{
|
|
float moveX = Input.GetAxisRaw("Horizontal");
|
|
animator.SetFloat("Horizontal", moveX);
|
|
float moveY = 0;
|
|
|
|
var groundCheck = Physics2D.CapsuleCast(_capsule.bounds.center,_capsule.bounds.size,CapsuleDirection2D.Vertical,0f, Vector2.down, .1f,groundLayer);
|
|
|
|
if (groundCheck || _isOnLadder)
|
|
{
|
|
var ladderCheck = Physics2D.CapsuleCast(_capsule.bounds.center, _capsule.bounds.size, CapsuleDirection2D.Vertical, 0f, Vector2.down, .1f, ladderLayer);
|
|
if (ladderCheck)
|
|
{
|
|
_isOnLadder = true;
|
|
moveY = Input.GetAxisRaw("Vertical");
|
|
if(moveY> 0)
|
|
{
|
|
if(!Physics2D.CapsuleCast(_capsule.bounds.center, _capsule.bounds.size, CapsuleDirection2D.Vertical, 0f, Vector2.up, .1f, ladderLayer))
|
|
{
|
|
moveY= 0;
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
_isOnLadder = false;
|
|
}
|
|
_body.velocity = new Vector2(moveX * MovementSpeed, moveY * MovementSpeed);
|
|
}
|
|
}
|
|
|
|
private void FixedUpdate()
|
|
{
|
|
if (_isOnLadder)
|
|
{
|
|
|
|
_body.gravityScale = 0;
|
|
}
|
|
else
|
|
{
|
|
_body.gravityScale = 5;
|
|
}
|
|
}
|
|
}
|