Hello.
I’m having some issues with my animations where the wrong one is showing at the wrong time and it’s making my game act funny.
I’ve put a link to a video of the problem below:
https://drive.google.com/file/d/1SK6hFZXlifKscjL33Z58Q3nMwh2aSdCD/view?usp=sharing
and below is my code for my PlayerMovement.
Thanks.
Sam.
public class PlayerMovement : MonoBehaviour
{
Vector2 moveInput;
Rigidbody2D myRigidbody;
Animator myAnimator;
CapsuleCollider2D myCapsuleCollider;
float gravityScaleAtStart;
Physics2D myPhysics;
[SerializeField] float runSpeed = 10f;
[SerializeField] float jumpSpeed = 10f;
[SerializeField] float climbSpeed = 5f;
// Start is called before the first frame update
void Start()
{
myRigidbody = GetComponent<Rigidbody2D>();
myAnimator = GetComponent<Animator>();
myCapsuleCollider = GetComponent<CapsuleCollider2D>();
gravityScaleAtStart = myRigidbody.gravityScale;
}
// Update is called once per frame
void Update()
{
Run();
FlipSprite();
ClimbLadder();
}
void ClimbLadder()
{
if (!myCapsuleCollider.IsTouchingLayers(LayerMask.GetMask("Climbing")))
{
myRigidbody.gravityScale = gravityScaleAtStart;
myAnimator.SetBool("isClimbing", false);
return;
}
Vector2 climbVelocity = new Vector2(myRigidbody.velocity.x, moveInput.y * climbSpeed);
myRigidbody.velocity = climbVelocity;
myRigidbody.gravityScale = 0f;
bool playerHasVerticalSpeed = Mathf.Abs(myRigidbody.velocity.y) > Mathf.Epsilon;
myAnimator.SetBool("isClimbing", playerHasVerticalSpeed);
}
void OnMove(InputValue value)
{
moveInput = value.Get<Vector2>();
Debug.Log(moveInput);
}
void OnJump(InputValue value)
{
if (!myCapsuleCollider.IsTouchingLayers(LayerMask.GetMask("Ground")))
{
return;
}
if (value.isPressed)
{
myRigidbody.velocity += new Vector2(0f, jumpSpeed);
}
}
void Run()
{
Vector2 playerVelocity = new Vector2(moveInput.x * runSpeed, myRigidbody.velocity.y);
myRigidbody.velocity = playerVelocity;
bool PlayerHasHorizonalSpeed = Mathf.Abs(myRigidbody.velocity.x) > Mathf.Epsilon;
myAnimator.SetBool("isRunning", PlayerHasHorizonalSpeed);
}
void FlipSprite()
{
bool PlayerHasHorizonalSpeed = Mathf.Abs(myRigidbody.velocity.x) > Mathf.Epsilon;
if (PlayerHasHorizonalSpeed)
{
transform.localScale = new Vector2(Mathf.Sign(myRigidbody.velocity.x), 1f);
}
}
}