- Home /
,How to stop jump animtion
Hello, I'm new to unity and I'm having this problem. I'm trying to figure out how to make it so my player will stop playing its jump animation while it is colliding with the ground but I am still not sure on how to write that code. It might be that I'm writing it wrong or something else but I have already tried to say if its grounded, but that did nothing at all. I have also tried putting it in other stops of my code to see if it would work but that did nothing at all. I already have to jumping animation set its just I want to know how to stop it.
My player code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PM : MonoBehaviour
public float moveSpeed;
public float jumpHeight;
Rigidbody2D rb;
BoxCollider2D boxColliderPlayer;
int layerMaskGround;
float heightTestPlayer;
public Animator animator;
void Start()
{
rb = GetComponent<Rigidbody2D>();
// Get the player's collider so we can calculate the height of the character.
boxColliderPlayer = GetComponent<BoxCollider2D>();
// We do the height test from the center of the player, so we should only check
// halft the height of the player + some extra to ignore rounding off errors.
heightTestPlayer = boxColliderPlayer.bounds.extents.y + 0.05f;
// We are only interested to get colliders on the ground layer. If we would
// like to jump ontop of enemies we should add their layer too (which then of
// course can't be on the same layer as the player).
layerMaskGround = LayerMask.GetMask("Ground");
}
void Update()
{
/// Player movement and playing the animtion
float moveDir = Input.GetAxis("Horizontal") * moveSpeed;
rb.velocity = new Vector2(moveDir, rb.velocity.y);
animator.SetFloat("Speed",Mathf.Abs(moveDir));
// Your jump code:
if (Input.GetKeyDown("space") && IsGrounded() )
{
rb.velocity = new Vector2(rb.velocity.x, jumpHeight);
////////// Telling it to play my jump anitmion animator.SetBool("isJump", true); }
//Telling it to rotate my animtion's
Vector3 characterScale = transform.localScale;
if (Input.GetAxis("Horizontal")< 0){
characterScale.x = -3;
}
if (Input.GetAxis("Horizontal")> 0){
characterScale.x = 3;
}
transform.localScale = characterScale;
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.CompareTag("Coins"))
{
Destroy(other.gameObject);
}
}
/// <summary>
/// Simple check to see if our character is no the ground.
/// </summary>
/// <returns>Returns <c>true</c> if the character is grounded.</returns>
private bool IsGrounded()
{
// Note that we only check for colliders on the Ground layer (we don't want to hit ourself).
RaycastHit2D hit = Physics2D.Raycast(boxColliderPlayer.bounds.center, Vector2.down, heightTestPlayer, layerMaskGround);
bool isGrounded = hit.collider != null;
// It is soo easy to make misstakes so do a lot of Debug.DrawRay calls when working with colliders...
Debug.DrawRay(boxColliderPlayer.bounds.center, Vector2.down * heightTestPlayer, isGrounded ? Color.green : Color.red, 0.5f);
return isGrounded;
} }
If I understood it correctly, your function "isGrounded()" doesn't work as you like. Up to know, your code seems ok. But have you tried to Debug your "isGrounded" var? Or simply look into your scene view and tell us, if the raycast is green or red. If it's red, your "isGrounded()" function should be correct and there must be an issue in your "Update()" function.
One more question: What do you mean with "the jump is not stopping". Is the player jumping all the time or do you mean only the jump-animation?
If you mean the last, then you have an issue in your code, because you don't set your speed val of your animator back to 0!
I have not many time to test it, but here my be some help: add a new variable isJumping:
bool isJumping = false;
Whenever you start to jump, set it to true:
if (Input.GetKeyDown("space") && IsGrounded() ) { isJu$$anonymous$$g = true; //... }
And when you are on ground AND isJumping is true, stop the animation:
private bool IsGrounded(){ //... if(isGrounded && isJumping){ isJumping = false; animator.SetFloat("Speed",0); } //... }
Hope it helps!
Answer by marcosmalin · Apr 19, 2021 at 07:40 AM
Hello!
as far as I can tell, you are setting your jump animation with this line right ? animator.SetFloat("Speed",Mathf.Abs(moveDir));?
where you check it against 0 and if it's != 0 you make it play the animation? because I don't see an animator.SetBool("isJump", true); line in your code.
Anyway, if you are using animator.SetBool("isJump", true); you need to put it animator.SetBool("isJump", false);
Do this by checking (!isGrounded) or (isGrounded == false) { animator.SetBool("isJump", false); }
Hope this helps!