Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by xandermacleod · Jun 15, 2014 at 07:52 PM · c#jumpcharacter controllerisgrounded

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.

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image xandermacleod · Jun 15, 2014 at 08:20 PM 0
Share

im using unity 4.5.1 if that helps diagnose the issue too.

avatar image xandermacleod · Jun 15, 2014 at 08:26 PM 0
Share

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?

avatar image AndyMartin458 · Jun 15, 2014 at 08:31 PM 0
Share

@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.

3 Replies

· Add your reply
  • Sort: 
avatar image
0

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;
     }
 }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image xandermacleod · Jun 15, 2014 at 08:11 PM 0
Share

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?

avatar image AndyMartin458 · Jun 15, 2014 at 08:26 PM 0
Share

@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);
     //...
 }
avatar image zeropointblack · Jun 23, 2020 at 04:02 AM 0
Share

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.

avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

25 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges