Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 blpup · Apr 07, 2015 at 01:35 AM · c#jumpball

Jumping Ball

Im trying to get my ball to jump but sometimes it just doesnt jump. The boolean turns it self off when hitting the spacebar but doesnt jump and stays on the ground. Here's my script.

 bool canJump = false;
 void OnCollisionEnter(Collision other)
     {
         if (other.gameObject.tag == "ground") {
             canJump = true;
             Debug.Log("test");
         }
     }
     void Movement()
     {
 
         move = Input.GetAxis ("Horizontal");
         rigidbody.velocity = new Vector3 (move * 5f, rigidbody.velocity.y);
         
         if(Input.GetKeyDown("space"))
         {
             if(canJump)
             {
             rigidbody.AddForce(new Vector3(0,10),ForceMode.Impulse);
                 canJump =false;
             }
 
         }
     }
Comment
Add comment
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

1 Reply

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

Answer by _Gkxd · Apr 07, 2015 at 02:08 AM

A reliable way to code jumping is to cast a ray downwards for a short distance and see if it hits the ground.

The code that you have assumes that whenever you press the jump key, you leave the collision with the ground, and after some time, you will enter collision again, triggering OnCollisionEnter. This isn't always true though.

If you press the jump key but don't leave collision with the ground, then you won't enter collision with the ground, meaning that canJump won't be set to true even if you're on the ground. For example, if you try to jump, but you're blocked by a ceiling and don't actually leave the ground, you won't be able to jump anymore. (This is definitely a problem that can happen in your code, but I'm not sure if it's the problem that you're having right now.)

The general raycasting idea is to use Physics.Raycast with a ray from your player's position downwards, setting maxDistance appropriately so that the ray ends at the bottom of your player. This ray should ideally collide with the ground only when your player is touching the ground.

When you press the jump key, set some boolean isInAir to true. Whenever isInAir is true, perform the raycast, and if the ray hits the ground, set isInAir to false.

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 blpup · Apr 07, 2015 at 07:42 AM 0
Share

But since it is a ball and is in constant turning motion, wont casting a Physics.Raycast downward turn the Ray left/right/up thus making the ray not work depending on how you land?

avatar image _Gkxd · Apr 07, 2015 at 12:32 PM 0
Share

The documentation for raycast specifies that starting point of the ray is in world coordinates, so it would make sense if the direction is in world orientation. You can always use Debug.DrawRay to see what ray you are casting in the editor.

I would find it strange if the direction is affected by the orientation of the object that you're casting from.

avatar image blpup · Apr 07, 2015 at 02:06 PM 0
Share

I used your method and placed 4 raycast's. I kinda already knew the method but i just thought maybe there was another way. Accpeted your method as Answer :)

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to make a ball jump? 2 Answers

Player stucken in the wall while jumping 2D 1 Answer

Character not jumping high enough 1 Answer


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