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
1
Question by samo8076 · Jun 27, 2012 at 03:49 PM · rigidbodyjumpball

Help with making ball jump

Hi guys im really new to Unity, spent the last few days trying to figure out how to make my ball jump using rigidbody. Sort of got it working, however it doesnt seem to fire the code everytime i press the space, there sometimes i have to press it 2-10 times to get the ball to jump. I spend ages crawling through posts and examples but nothing seems to work..below is my script. Thanks for your time.

 var speed = 20.0;
 var gravitypull = 0;
 var jumpSpeed = 5.0f;
      
      
 //function FixedUpdate() {
 function Update(){ 
     if (Input.GetKey ("space") && Physics.Raycast(transform.position, -transform.up,1)) 
     {
         rigidbody.velocity.y = jumpSpeed;
         //rigidbody.AddForce(Vector3.up * jumpSpeed, ForceMode.Impulse);
     }
      
     if (Input.GetKey ("right"))
         rigidbody.AddForce(Vector3.right * speed * Time.deltaTime);
     }
  
     if (Input.GetKey ("left")) {
         rigidbody.AddForce(Vector3.right * -speed * Time.deltaTime);
     } 
 }
Comment
Add comment · Show 1
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 jellybit · Jun 27, 2012 at 05:42 PM 0
Share

I'm just a beginner, but did you try taking out the part that says:

 && Physics.Raycast(transform.position, -transform.up,1 

to see if the problem is there? Does "-transform.up" work? How big is the ball? Is one meter enough?

4 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by FLASHDENMARK · Jun 27, 2012 at 08:31 PM

As you are using a rigidbody and you use AddForce I suspect that your ball rotates. And you are using -transform.up in your raycast parameter which is relative to the rotation of the ball.

You should properly use -Vector3.up, like so:

 var speed = 20.0;
 var gravitypull = 0;
 var jumpSpeed = 5.0f;
 
 function Update(){ 
     if (Input.GetKey("space") && Physics.Raycast(transform.position, -Vector3.up, 1)) 
     {
         rigidbody.velocity.y = jumpSpeed;
         //rigidbody.AddForce(Vector3.up * jumpSpeed, ForceMode.Impulse);
     }
 
     if (Input.GetKey ("right")){
         rigidbody.AddForce(Vector3.right * speed * Time.deltaTime);
     }
 
     if (Input.GetKey ("left")){
         rigidbody.AddForce(Vector3.right * -speed * Time.deltaTime);
     } 
 }
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 slayer29179 · Jun 27, 2012 at 08:12 PM

To make objects jump I don't usually use raycast, I usually just use;

 var jumpSpeed : float = 8.0;
 var gravity : float = 20.0;
 var rotateSpeed : float = 3.0;
 
 private var moveDirection : Vector3 = Vector3.zero;
 
 function Update()
 {
      if (Input.GetButton ("Jump")) 
      {
         moveDirection.y = jumpSpeed;
      }
      // Apply gravity
      moveDirection.y -= gravity * Time.deltaTime;
 
     // Move the controller
     controller.Move(moveDirection * Time.deltaTime);
 }




Maybe you could just add this on the end? otherwise I don't know

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 FLASHDENMARK · Jun 27, 2012 at 08:34 PM 0
Share

This requires a CharacterController. That doesn't sound like the ideal choice for the task the OP faces.

But whatever floats the OP's boat, I guess.

avatar image
0

Answer by samo8076 · Jun 28, 2012 at 07:16 AM

Thanks OrangeLightning , that does the trick :)

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 Eks-Squared · May 07, 2014 at 06:28 AM

I just had this SAME PROBLEM. I did what OrangeLightning suggested and used Update() for button commands instead of FixedUpdate().

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

8 People are following this question.

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

Related Questions

Rigidbody jump adds bounce energy to second jump 1 Answer

How do I check when a rigidbody is colliding with the floor? 2 Answers

Jump Input Sensitivity 0 Answers

How do I make my character jump over a cube with rigid body moving towards it 1 Answer

Ball Addforce Acceleration Speed 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