Hello, this will be my first post here.
QQ, I’m using this script to control my player movement and the animations that will play for idle, walking, jumping, and falling.
For some reason, whenever the character walks to the left the falling animation will play instead of the walking animation.
I’ve tried changing the box collider to see if that was the issue as well as deleting this portion of the code:
else if (rb.velocity.y < -.1f)
{
state = MovementState.falling;
}
It stopped the falling animation from playing and the walking animation was able to play when the character walked to the left.
Any assistance would be greatly appreciated.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
// This variable is for accessing the Rigidbody2D class
private Rigidbody2D rb;
// This variable is for accessing the BoxCollider2D class
private CapsuleCollider2D coll;
// This variable is for accessing the SpriteRenderer class
private SpriteRenderer sprite;
// This variable is for accessing the Animator class
private Animator anim;
[SerializeField] private LayerMask jumpableGround;
private float dirX;
[SerializeField] private float moveSpeed = 7f;
[SerializeField] private float jumpForce = 14f;
[SerializeField] private float fallMultiplier = 5f;
private Vector2 vecGravity;
//
private enum MovementState { idle, walking, jumping, falling }
// Start is called before the first frame update
private void Start()
{ // This references the "rb" variable and tells it what Component to manipulate in the Inspector
rb = GetComponent<Rigidbody2D>();
// This references the "coll" variable and tells it what Component to manipulate in the Inspector
coll = GetComponent<CapsuleCollider2D>();
// This references the "sprite" variable and tells it what Component to manipulate in the Inspector
sprite = GetComponent<SpriteRenderer>();
// This references the "anim" variable and tells it what Component to manipulate in the Inspector
anim = GetComponent<Animator>();
vecGravity = new Vector2(0, -Physics2D.gravity.y);
}
// Update is called once per frame
private void Update()
{
dirX = Input.GetAxisRaw("Horizontal");
if (Input.GetButtonDown("Jump") && IsGrounded())
{
rb.velocity = new Vector2(rb.velocity.x, jumpForce);
}
if (rb.velocity.y < 0)
{
rb.velocity -= vecGravity * fallMultiplier * Time.deltaTime;
}
UpdateAnimationState();
}
private void FixedUpdate()
{
rb.velocity = new Vector2(dirX * moveSpeed, rb.velocity.y);
}
private void UpdateAnimationState()
{
MovementState state;
if (dirX > 0f)
{
state = MovementState.walking;
sprite.flipX = false;
}
else if (dirX < 0f)
{
state = MovementState.walking;
sprite.flipX = true;
}
else
{
state = MovementState.idle;
}
if (rb.velocity.y > .1f)
{
state = MovementState.jumping;
}
else if (rb.velocity.y < -.1f)
{
state = MovementState.falling;
}
anim.SetInteger("state", (int)state);
}
private bool IsGrounded()
{
return Physics2D.BoxCast(coll.bounds.center, coll.bounds.size, 0f, Vector2.down, .1f, jumpableGround);
}
}