- Home /
isGrounded not working
I have an extremely simple scene with the following setup.
1 cube object starts in the air and has a Box Collider, Character Controller, Rigidbody (with freeze rotations checked) and a Jump script I will get to later.
Beneath it (with a fair amount of space between it and the hovering cube) there exists a floor cube. This floor cube has a box collider.
Neither of the two objects box colliders have isTrigger checked.
The Jump script looks as follows:
using UnityEngine;
using System.Collections;
public class Jump : MonoBehaviour {
public float JumpPower;
private CharacterController controller;
void Start()
{
controller = GetComponent<CharacterController>();
}
void Update()
{
if(Input.GetKeyDown("space"))
{
Debug.Log("space pressed");
if(controller.isGrounded)
{
Debug.Log("grounded");
gameObject.rigidbody.AddForce(Vector3.up * JumpPower);
}
}
}
}
So when the scene starts, my cube falls to the floor as expected.
Then when I press space, I get the message 'space pressed' but not 'grounded'. It appears that the is.Grounded check always turns up false. Can someone explain why to me? There's literally no other scripts or objects in the scene. I'm pretty confused.
im using unity 4.5.1 if that helps diagnose the issue too.
O$$anonymous$$ I'm beginning to think it must be that I have a box collider and a character controller on my hovering cube. When I remove the box collider, my cube falls through the floor. And then if I remove the rigidbody I can no longer benefit from unity's physics gravity. But if I get rid of the character collider I no longer get access to isGrounded checks. any solutions?
@xandermacleod It can get pretty cumbersome how Unity components don't always work well together. Even with the way that you want to have multiple types of ground, like a 2-d jumping game, you could do your own collision detection like I showed. Just mark anything that can be considered as ground, with the ground tag: floors, pillars, islands, etc.
Answer by AndyMartin458 · Jun 15, 2014 at 07:59 PM
According to the documentation, this returns true if "Was the CharacterController touching the ground during the last move?" is a true statement. Have you tried moving the character first, as opposed to simply letting it hit the ground. You could write your own isGrounded functionality as well, and that might be the best option.
//not checked for compilation, this is just an example
private bool isGrounded = false;
void OnCollisionEnter(Collision collision)
{
if(collision.tag == "ground")
{
isGrounded = true;
}
}
if possible I'd rather us .isGrounded and collisionFlag tests because it will matter later on if my character jumps on-top of an object or goes sideways into it (like $$anonymous$$ario behaves with enemies). And Id rather not have to check for normal angles of collision boxes etc if possible, given that isGrounded is supposed to work).
I'm a bit puzzled about why this isn't working as it should...
I haven't tried moving it yet. What kind of movement are you thinking anyway? just drag the box up quickly after pressing play? sideways force? direct control over setting it's transform position?
@xandermacleod I think that they are expecting you to move the character with CharacterController.$$anonymous$$ove http://docs.unity3d.com/ScriptReference/CharacterController.$$anonymous$$ove.html
I think that a good trick would be to call that function maybe this way where it would not actually move the character but would register as a move.
void Start()
{
//...
controller.$$anonymous$$ove(Vector3.zero);
//...
}
are you kidding bro, its obvious this is a test scene he made to debug this broken functionality. im sure hes jumped many times before during and after posting this, in this project and in others. isGrounded just doesnt work. anyone told the devs yet? i think it would be something theyd like to know about.
Answer by AnkitRajpoot · Sep 14, 2020 at 11:00 AM
you can not use void OncollisionEnter with character controller .on the behalf of oncollisionenter you can use oncontrollercolliderhit
Answer by logicandchaos · Sep 14, 2020 at 02:11 PM
bool isGrounded = false;
void OnCollisionEnter(Collision collision)
{
if(collision.tag == "ground")
{
isGrounded = true;
}
}
void OnCollisionExit(Collision collision)
{
if(collision.tag == "ground")
{
isGrounded = false;
}
}
now if you want to do different behaviour for if you hit the side or not I would use multiple colliders on your objects with different tags, you could have 3 stacked up like a hamburger, top, middle, bottom, then you just check if you collide with top, middle or bottom.
Your answer
Follow this Question
Related Questions
,How to disable autojumping when holding spacebar? 4 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Extend simple movement script (C#) 1 Answer
have 3rd person make a side jump -1 Answers