Hey guys!
I’ve set up a Blend tree for a jump and fall animation in my animator with works well so far.
The problem I have is that in my script I set an “IsJumping”-boolean to true when he leaves the ground and now I am struggling to find a place in my code where I can set the Boolean to “false”.
Here’s what it looks like:
public class PlayerMovement : MonoBehaviour
{
[SerializeField] float playerSpeed = 5f;
[SerializeField] float jumpSpeed = 5f;
[SerializeField] float climbSpeed = 5f;
private Vector2 moveInput;
private Rigidbody2D myRigidbody;
private Animator myAnimator;
private CapsuleCollider2D myBodyCollider;
private BoxCollider2D myFeetCollider;
void Start()
{
myRigidbody = GetComponent<Rigidbody2D>();
myAnimator = GetComponent<Animator>();
myBodyCollider = GetComponent<CapsuleCollider2D>();
myFeetCollider = GetComponent<BoxCollider2D>();
}
// Update is called once per frame
void Update()
{
Run();
FlipSprite();
ClimbLadder();
SetYVelocity();
//myAnimator.SetBool("isJumping", false);
}
void OnMove(InputValue value)
{
moveInput = value.Get<Vector2>();
}
void OnJump(InputValue value)
{
if (!myFeetCollider.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 * playerSpeed, myRigidbody.velocity.y);
myRigidbody.velocity = playerVelocity;
bool playerHasHorizontalSpeed = Mathf.Abs(myRigidbody.velocity.x) > Mathf.Epsilon;
if (playerHasHorizontalSpeed)
{
myAnimator.SetBool("isRunning", true);
}
else
{
myAnimator.SetBool("isRunning", false);
}
}
void FlipSprite()
{
bool playerHasHorizontalSpeed = Mathf.Abs(myRigidbody.velocity.x) > Mathf.Epsilon;
if (playerHasHorizontalSpeed)
{
transform.localScale = new Vector2 (Mathf.Sign(myRigidbody.velocity.x), 1f);
}
}
void ClimbLadder()
{
if (!myFeetCollider.IsTouchingLayers(LayerMask.GetMask("Climbing"))) { return; }
Vector2 climbVelocity = new Vector2 (myRigidbody.velocity.x, moveInput.y * climbSpeed);
myRigidbody.velocity = climbVelocity;
}
void SetYVelocity()
{
myAnimator.SetFloat("yVelocity", myRigidbody.velocity.y);
}
}
If someone has some advice for me that would be ace!
Thanks!