- Home /
Jump animation only plays for about 1/10 of a second unless the button is pressed twice
I just started learning how to use Unity. I'm working on a 2D platformer using tutorial videos from the youtube channel "Brackeys". My method is to go through one video, following his instructions to a T, and repeating that same video through multiple iterations before moving on to the next video. For a reason that i cant figure out, one of my iterations wont properly play the jumping animation.
In the animation preview window it looks perfectly fine, but when doing a test run the animation will only play for a split second before reverting back to the animation before it (either run or idle). Although it will play all the way through if I press the button twice. When looking at the parameters tab in the animation window, I can see the "IsJumping" parameter is activated for that split second when i press the jump button but then it unchecks immediately after. The weirdest part is that I've checked about five times and as far as I can see everything is exactly the same between the iteration that doesnt work and the one that does. Any help or advice would be greatly appreciated!
Here is the movement script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Playermovement : MonoBehaviour {
public CharacterController2D controller;
public Animator animator;
public float runSpeed = 40f;
float horizontalMove = 0f;
bool jump = false;
bool crouch = false;
// Update is called once per frame
void Update() {
horizontalMove = Input.GetAxisRaw("Horizontal") * runSpeed;
animator.SetFloat("Speed", Mathf.Abs(horizontalMove));
if (Input.GetButtonDown("Jump"))
{
jump = true;
animator.SetBool("IsJumping", true);
}
if (Input.GetButtonDown("Crouch"))
{
crouch = true;
} else if (Input.GetButtonUp("Crouch"))
{
crouch = false;
}
}
public void OnLanding()
{
animator.SetBool("IsJumping", false);
}
public void OnCrouching (bool isCrouching)
{
animator.SetBool("IsCrouching", isCrouching);
}
void FixedUpdate ()
{
// Move our character
controller.Move(horizontalMove * Time.fixedDeltaTime, crouch, jump);
jump = false;
}
}
Answer by casemer · Feb 09, 2020 at 03:29 AM
@Trevor_trev I figured it out! My issue was not in the PlayerMovement, it is in the CharacterController. In the FixedUpdate, there is this part:
m_Grounded = true;
if (!wasGrounded)
OnLandEvent.Invoke();
}
get rid of that "!" I am not sure why his code had it but it should not be there.
m_Grounded = true;
if (wasGrounded)
OnLandEvent.Invoke();
}
Hope this was the same issue for you and this fixes it!
Awesome! Thanks! I thought this question was too buried for anyone to answer so I didn't check back until now haha.
I was looking at the same code and tried to remove the '!' character. However, the bug remains the same...
$$anonymous$$e too... @teruyakimura did you find the solution??
Yes! Actually, I seached the issue in the YT commentary section, and what worked for me was change the variable value k_GroundedRadius to .05f. It is located in the Controller2D.
I am experiencing the same problem as you, and also following Bracky's tutorial. I cant find the code that says:
m_Grounded = true;
if (!wasGrounded)
OnLandEvent.Invoke();
}
Where is it? Thank you!
Hi, i know it been over a year, but now its my time and i found this... and i found out that just deleting the "!" from !wasGrounded does nothing else than repeating the OnLandEvent... i found this out by adding a sound to the landing animation and was wondering why i hear this ticking all the time... it was the ever repeating sound of the landing animation.... Which now fires all the time and every frame again, that you dont see the animation but hear the beginning of the sound.
Well, i found this thread because my character startedt (right today with no warning) to DONT show the jump animation anymore... instead he was just standing and jumping up in idle... with the second hit of the spacebar i could see the animation....
If anyone has found out whats going on here please let me know!
But know: Its not just deleting an ! and its all good - thats the bad way :) thanks guys!
Answer by NcNtt · Feb 19, 2021 at 09:02 PM
And here i am again - with an answer - in the CharacterController2D Script, that all of you, who rebuilt the Brackeys stuff, should have - there is a section called:
// If the player should jump...
if (m_Grounded && jump)
{
// Add a vertical force to the player.
m_Grounded = false;
m_Rigidbody2D.AddForce(new Vector2(0f, m_JumpForce));
}
Please correct the false to true... Reason: The script checks constantly if the Player isGrounded - which you have to set by giving the component a material or a collider and set this to "WhatIsGround" - you should have this in the Inspector window under your CharacterController2D script as a float.
Now, the script asks the following:
If the Player is grounded and enters jump state please AddForce to the Rigidbody called JumpForce. (also to be set in the inspector.
Now the script also says, which is funny - m_Grounded=false; which doesnt make any sense at all. So in my case it only showed the jump anymation when im in midair, so to say NOT GROUNDED - which was correct in terms of the script.
So please set it to
m_Grounded = true;
and the problem should be corrected.
This is my state of knowledge and i think there are many others out there knowing better - but hey - i wrote this! you not! ^^
Have fun!
Your answer
Follow this Question
Related Questions
Choose Start and End Frame for 2D Animation 0 Answers
Even / Constant Speed and Jumping 2D 1 Answer
3DBuzz Third Person Character Jumping And Landing Issue 0 Answers
Animation Parameters Not Working Correctly 0 Answers
Animation of the death (2D Game) 0 Answers