Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by Kadeo · Jun 06, 2016 at 07:19 PM · charactercontrollerjumping objectisgrounded

Making a rolling ball jump, but only when it is touching the ground

I know that a lot of these questions have already been asked, but I have looked through many, and none of them seem to work for me. I have a ball that is rolling, so it's y axis is not always straight up. I need it to increase it's y axis (global). I have gotten this to work, but as soon as I add in the character controller, it stops jumping. Here is my code, any help is appreciated.

This section is under Update, and the groundContact variable is set.

Code is in C#

         {
             CharacterController controller = GetComponent<CharacterController>();
             if (controller.isGrounded)
                 groundContact = true;
         }
 
         if (Input.GetKeyDown(KeyCode.Space) && groundContact)
             rb.AddForce(new Vector3(0, 10, 0), ForceMode.Impulse);
Comment
Add comment · Show 2
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 RayCastX · Jun 06, 2016 at 07:36 PM 0
Share

the CharacterController doesn't use a Rigidbody

avatar image Kadeo · Jun 07, 2016 at 01:57 AM 0
Share

Just making sure, so what you are saying is that I will have to use another way to set groundContact to true other than CharacterController to get this to work correctly? If so, is a rigid body the best way to do this?

5 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Kadeo · Jun 08, 2016 at 10:58 AM

Thank you everyone for helping me, I have figured it all out, it works great now. I have ended up using a rigidbody collider and trigger to get it to jump only when touching the ground. All of the replies so far has given me a little advice that has helped me figure it out. I will post my code below just in case someone else needs it.

Code is in C#

     void Update()
     {
         if (Input.GetKey (KeyCode.Space) && groundContact) 
         {
             rb.AddForce (new Vector3 (0, 10, 0), ForceMode.Impulse);
             groundContact = false;
         }
     }
 void OnTriggerEnter(Collider other)
     {
         if (other.gameObject.CompareTag ("Ground"))
         {
             groundContact = true;
         }
     }

Now to get to the finer details of the jump.

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
1

Answer by nelson218 · Jun 08, 2016 at 03:51 PM

Simply use these

  groundContact = controller.isGrounded;

instead of these

     if (controller.isGrounded)
              groundContact = true;



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 melwei · Jun 07, 2016 at 04:33 PM

I think you should set groundContact to false at the end of your Update, or it will continouosly be true after jumping once. If controller.isGrounded doesn't work (Test with Debug?), try to shoot a raycast down.

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 IAcontrol · Jun 07, 2016 at 07:05 PM

     public Transform GroundCheck;
     public LayerMask WhatIsGround;
     float GroundRadius = 0.2f;
     bool Grounded = false;
 
     void FixedUpdate ()
         {
             Grounded = Physics2D.OverlapCircle(GroundCheck.position, GroundRadius, WhatIsGround);
     }

I used this code for make my 2D game, when the sprite touch the floor set the bool true. You have to create an empty child game object.

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 solo365 · Jan 07, 2018 at 05:54 PM

I am quite new to this field.Can you please tell me how can I do the same thing with my code. I also want my sphere to jump again after it touches the ground. Here's my code.

 void Update() 
     {
         if (
             !thrown && (
                 (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended ) ||
                 Input.GetMouseButtonDown(0) )
         )
         {
             rb.isKinematic = false;
             rb.AddForce (new Vector3(0.0f, 0.5f, 0.2f));
             thrown = true ;
 
         }
     }
     void OnTriggerEnter(Collider other)
     { 
         if (other.gameObject.CompareTag ("player")) 
         {
             thrown = true;
         }
     }

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

9 People are following this question.

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

Related Questions

Using isGrounded with a RigidBody 2 Answers

How to make character controller forward jump a certain distance? 1 Answer

Why does my characterController code not apply gravity correctly? 0 Answers

How can I edit this script, that it will check if the character is standing on a specific layer? 0 Answers

Charactercontroller.isGrounded never triggers 0 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