IsGrounded not updating properly
Hi Guys,
I am using controller.isGrounded to check if the player is touching the ground to check if he can jump or not. Most of the times it works, but 10% of the time it doesn't detect I am off the ground to start the jump animation. Is there another way to do this wich is more reliable?
void Update()
{
CharacterController controller = GetComponent<CharacterController>();
if(controller.isGrounded)
{
grounded = true;
}
if (!controller.isGrounded)
{
grounded = false;
}
if (Input.GetKeyDown(KeyCode.Space) && grounded == true)
{
print("We are grounded");
anim.SetBool("Jumping", true);
}
else
{
anim.SetBool("Jumping", false);
}
}
Answer by hexagonius · Nov 15, 2016 at 09:21 PM
CharacterController controller;
void Awake() {
controller = GetComponent<CharacterController>();
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space) && controller.Grounded)
{
print("We are grounded");
anim.SetBool("Jumping", true);
}
else
{
anim.SetBool("Jumping", false);
}
}
just thought i'd shorten your code a bit, but what exactly is the outcome of your problem?
just a thought though. you could make your Jumping bool a trigger and call SetTrigger instead of SetBool, which comes more natural to a jump. would also remove the else part of your code.
The problem is it doesn't always trigger the animation cause of it not seeing correctly if its grounded or not. This way sometimes my character jumps, but doesnt animate. I work with the third person controller script from unity itselves. $$anonymous$$aybe that is the problem.
Your answer
Follow this Question
Related Questions
Loot table problems 1 Answer
Game crashes on iPhone 11 0 Answers
Need help with Mute Button 1 Answer
How do i have game object in the middle of a Catmull spline? 0 Answers
how to move a rigidbody ball or kick a ball from swipe gesture 0 Answers