• 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 create3dgames · Jun 26, 2011 at 01:50 AM · 2dphysicsjumpbounce

Sphere Goes Crazy

I have a ball that has a sphere collider and a rigidbody in my scene.

Here's my script for the ball:

 var jumpSpeed : float = 8.0;
 
 
 function Update()
 
 {
 
  if (Input.GetButton ("Jump"))
      
 {
 transform.Translate (Vector3.up * Time.deltaTime * jumpSpeed);
 }
 
 }



When I press play, everything appears to be fine, but when I press space to jump, the ball goes all over the place and goes backwards and crazy!

I do not want to use a character controller, by the way. I want my ball to be bouncy and for that I need a Physic Material unless someone can think of some way to put a physic material on a character controller.

Also, my game is a 2d game.

Thanks in advance,

Elijah.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
4

Answer by FourSheds · Jun 26, 2011 at 06:41 AM

Hi Elijah,

Coincidentally, I'm teaching my 13 y.o. son Unity, so complete respect if you're learning this on your own!

To get the ball to jump via Physics, you could start with the following:

 var jumpForce : float = 8.0;
 function Update () {
     
     if (Input.GetButtonDown ("Jump"))
     {
         rigidbody.AddForce(Vector3.up * jumpSpeed);
     }
 }


If you've used the default values for a sphere and rigidbody, you'll probably find you need to make jumpForce quite large. This is because you're only adding a single impulse of force at one moment. For a ball, this may be what you want, but otherwise you can add the same force inside FixedUpdate() over several frames (e.g. by making a timer).

You'll also notice that the ball can jump many times by pressing the "Jump" key quickly, even when it's in the air. You could prevent this either by testing for its transform.position.y value, or by limiting how often the "Jump" key can be pressed with a timer.

Comment
Add comment · Show 1 · 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 Ipsquiggle · Jun 27, 2011 at 04:55 PM 1
Share

Note also that FourSheds used GetButtonDown(), not GetButton(). GetButton() will trigger every single frame that the jump button is pressed, but GetButtonDown() will only trigger once when the button is pressed. It has to be release and pressed again to trigger a second time.

avatar image
2

Answer by Bunny83 · Jun 26, 2011 at 03:18 AM

transform.Translate will actually bypass the physics. You have to use forces to move the rigidbody. The rigidbody have its own velocity property that gets affected by the forces.

Try something like `rigidbody.AddForce()`

Comment
Add comment · Show 2 · 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 create3dgames · Jun 26, 2011 at 03:50 AM 0
Share

How exactly would I go abou doing that? I mean, I'm really not very good at scripting. ( I'm only 13 years old)

avatar image Waz · Jun 26, 2011 at 06:09 AM 1
Share

Exactly as you have used Translate, and as the linked documentation shows.

avatar image
1

Answer by hypnoslave · Jun 26, 2011 at 05:33 AM

change Input.GetButton to Input.GetButtonDown.

Input.GetButton will return true for every frame that the space bar is held down. if you want to just do a quick jump, change to GetButtonDown -- that will only return true on the frame that you actually pressed spacebar.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

7 People are following this question.

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

Related Questions

Ball bounce problem 1 Answer

Unity2D Collision is bugged 0 Answers

How do I increase forward velocity of a 2D ball when it bounces? 1 Answer

How to make objects "Bounceable" 1 Answer

Problem with joints 2 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