Animations imp.
This commit is contained in:
@@ -8,7 +8,7 @@ public class PlayerController : MonoBehaviour
|
||||
private float MovementSpeed = 1.5f;
|
||||
[SerializeField]
|
||||
private LayerMask ladderLayer;
|
||||
[SerializeField]
|
||||
[SerializeField]
|
||||
private LayerMask groundLayer;
|
||||
[SerializeField]
|
||||
private float distance;
|
||||
@@ -16,36 +16,46 @@ public class PlayerController : MonoBehaviour
|
||||
private Rigidbody2D _body;
|
||||
private BoxCollider2D _boxCollider;
|
||||
private bool _isFall;
|
||||
private bool _facingRight = true;
|
||||
|
||||
|
||||
void Start()
|
||||
{
|
||||
_body = GetComponent<Rigidbody2D>();
|
||||
_boxCollider= GetComponent<BoxCollider2D>();
|
||||
_boxCollider = GetComponent<BoxCollider2D>();
|
||||
}
|
||||
void Update()
|
||||
{
|
||||
float moveX = 0;
|
||||
float moveY = 0;
|
||||
|
||||
var groundCheck = Physics2D.BoxCast(_boxCollider.bounds.center,_boxCollider.bounds.size,0f, Vector2.down, .1f,groundLayer);
|
||||
|
||||
float inputHorizontal = 0;
|
||||
float inputVertical = 0;
|
||||
|
||||
var groundCheck = Physics2D.BoxCast(_boxCollider.bounds.center, _boxCollider.bounds.size, 0f, Vector2.down, .1f, groundLayer);
|
||||
|
||||
if (groundCheck || _isOnLadder)
|
||||
{
|
||||
|
||||
_isFall = false;
|
||||
inputHorizontal = Input.GetAxisRaw("Horizontal");
|
||||
if (inputHorizontal > 0 && !_facingRight)
|
||||
{
|
||||
FlipCharacter();
|
||||
}
|
||||
if (inputHorizontal < 0 && _facingRight)
|
||||
{
|
||||
FlipCharacter();
|
||||
}
|
||||
|
||||
_isFall =false;
|
||||
moveX = Input.GetAxisRaw("Horizontal");
|
||||
animator.SetFloat("Horizontal", moveX);
|
||||
animator.SetBool("PlayerWalk", inputHorizontal != 0);
|
||||
|
||||
if (IsLadder(Vector2.down))
|
||||
{
|
||||
_isOnLadder = true;
|
||||
moveY = Input.GetAxisRaw("Vertical");
|
||||
if(moveY> 0)
|
||||
inputVertical = Input.GetAxisRaw("Vertical");
|
||||
|
||||
if (inputVertical > 0)
|
||||
{
|
||||
if (!IsLadder(Vector2.up))
|
||||
{
|
||||
moveY = 0;
|
||||
inputVertical = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -53,12 +63,11 @@ public class PlayerController : MonoBehaviour
|
||||
{
|
||||
_isOnLadder = false;
|
||||
}
|
||||
_body.velocity = new Vector2(moveX * MovementSpeed, moveY * MovementSpeed);
|
||||
_body.velocity = new Vector2(inputHorizontal * MovementSpeed, inputVertical * MovementSpeed);
|
||||
}
|
||||
else
|
||||
{
|
||||
_isFall = true;
|
||||
print("Falling");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -71,7 +80,7 @@ public class PlayerController : MonoBehaviour
|
||||
{
|
||||
if (_isOnLadder)
|
||||
{
|
||||
|
||||
|
||||
_body.gravityScale = 0;
|
||||
}
|
||||
else
|
||||
@@ -80,8 +89,21 @@ public class PlayerController : MonoBehaviour
|
||||
}
|
||||
if (_isFall)
|
||||
{
|
||||
if (IsLadder(Vector2.down))
|
||||
{
|
||||
_isOnLadder = true;
|
||||
}
|
||||
_body.velocity = new Vector2(0, _body.velocity.y);
|
||||
animator.SetFloat("Horizontal", 0);
|
||||
animator.SetBool("PlayerWalk", false);
|
||||
}
|
||||
}
|
||||
|
||||
private void FlipCharacter()
|
||||
{
|
||||
Vector3 currentScale = gameObject.transform.localScale;
|
||||
currentScale.x *= -1;
|
||||
gameObject.transform.localScale = currentScale;
|
||||
|
||||
_facingRight = !_facingRight;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user