Finally got it to sort of work! What I mean is that the animation and states themselves do get triggered as they should, but the jump animation is very ‘basic’ in that you have no animation states for when you fall from very steep heights, in which case the character simply loops the default jumping animation over and over. But of course this is a whole different topic and not quite related to this particular problem from before. This is how I got it to work:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerMovement : MonoBehaviour
{
Vector2 moveInput;
Rigidbody2D myRigidbody;
Animator myAnimator;
CapsuleCollider2D myCapsuleCollider;
[SerializeField] float runSpeed = 10f;
[SerializeField] float jumpSpeed = 5f;
void Start()
{
myRigidbody = GetComponent<Rigidbody2D>();
myAnimator = GetComponent<Animator>();
myCapsuleCollider = GetComponent<CapsuleCollider2D>();
myAnimator.SetBool("isRunning",false);
}
void Update()
{
Run();
FlipSprite();
bool playerHasVSpeed = Mathf.Abs(myRigidbody.velocity.y) > Mathf.Epsilon;
myAnimator.SetBool("isJumping",playerHasVSpeed);
}
void OnMove(InputValue value)
{
moveInput = value.Get<Vector2>();
}
void OnJump(InputValue value)
{
if (!myCapsuleCollider.IsTouchingLayers(LayerMask.GetMask("Ground")))
{
return;
}
if(value.isPressed)
{
myAnimator.SetBool("isJumping",true);
myRigidbody.velocity += new Vector2 (0f, jumpSpeed);
}
}
void Run()
{
Vector2 playerVelocity = new Vector2 (moveInput.x * runSpeed, myRigidbody.velocity.y);
myRigidbody.velocity = playerVelocity;
bool playerHasVSpeed = Mathf.Abs(myRigidbody.velocity.y) > Mathf.Epsilon;
bool playerHasHorizontalSpeed = Mathf.Abs(myRigidbody.velocity.x) > Mathf.Epsilon;
if(playerHasVSpeed && playerHasHorizontalSpeed)
{
myAnimator.SetBool("isRunning",false);
}
else
{
myAnimator.SetBool("isRunning",playerHasHorizontalSpeed);
}
}
void FlipSprite()
{
bool playerHasHorizontalSpeed = Mathf.Abs(myRigidbody.velocity.x) > Mathf.Epsilon;
if (playerHasHorizontalSpeed)
{
transform.localScale = new Vector2 (Mathf.Sign(myRigidbody.velocity.x),1f);
}
}
}
So basically in my update function I check if my character has vertical speed, and if so I simply use that bool variable to toggle the jumping state. Because the update function checks this every frame, the Jumping state is automatically turned off when there is no vertical speed. Of course there was also another issue where you have both X and Y velocities, in which case there would be a conflict of animation states between running and jumping. For that I went to the running script and added an exception wherein I turn off the isRunning state if the player also has vertical speed.
I realize this is not the best of fixes, but for now, I think I worked on this much more than I should have considering that in this part of the course the jump animation was not an aspect that was discussed.
Thank you so much for all your help!