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 Shocking · Jun 19, 2013 at 02:52 PM · jumpjumping

Jumping ball jumps at random height when I press Space

I'm trying to make a 2D ball physics puzzle game. Where the player controls a ball through a maze type thing.

When I make the ball jump, It sometimes jumps really high, and sometimes it barely gets off the floor.

Here is the code I am using, It is attached to the ball: using UnityEngine; using System.Collections;

 public class Control : MonoBehaviour {
     
     public GameObject Ball;
     public float JumpSpeed; 
     public Camera cam;
     public float runSpeed;
     
     // Use this for initialization
     void Start () {
         Time.timeScale = 5.0f;
     }
     
     // Update is called once per frame
     void Update () {    
         
     }
     
     void FixedUpdate () {
             // Jumping code:
         if (Input.GetButtonDown ("Jump") ) {
             Ball.rigidbody.AddForce (Vector3.up * JumpSpeed);
         }
         
         Ball.rigidbody.AddForce (-Vector3.left * (Input.GetAxis("Horizontal") * runSpeed));
         float camToX = Ball.transform.position.x;
         float camToY = Ball.transform.position.y;
         float camToZ = cam.transform.position.z;
         
         Vector3 moveCamTo = new Vector3(camToX,camToY, camToZ);
         cam.transform.position = moveCamTo;
     }
     
 }

Any help would be Greatly Appreciated! Also, If any one could help my with not double/multi jumping would be nice too but it isn't needed :)

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
1
Best Answer

Answer by robertbu · Jun 19, 2013 at 03:05 PM

You are adding a certain amount of force to the ball. If the ball is falling that force is first used to counteract the falling velocity before it can then sends it up (a small amount). If the ball is rising, the force adds to the already rising velocity. One solution is to zero out the current velocity before adding the new force. Just below line 20, insert:

 Vector3 v3 = rigidbody.velocity;
 v3.y = 0;
 rigidbody.velocity = v3;

Another solution would be to replace the AddForce with a direct assignment of velocity:

 Vector3 v3 = rigidbody.velocity;
 v3.y = JumpSpeed;
 rigidbody.velocity = v3;

Your jumpSpeed will be much smaller than the value you used for AddForce(). Note both solutions play with physics in a way that may make the ball appear less realistic.

As for double/multi jumping, one solution would be to only allow the jump if the ball was near/at the ground. If you are bouncing on a simple plane, you could change line 20:

   if (Input.GetButtonDown ("Jump") && Ball.transform.y < someValue) {

...where 'someValue' is a height with the ball just off the ground. If the surface is uneven, you'll have to use colliders or raycasting to determine if the ball is near the ground.

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 Shocking · Jun 19, 2013 at 04:12 PM 0
Share

Thank you! that did the trick for the random jump height. I used the second second solution :)

As for the double jump part, I was wondering if there was a way to tell if an object has entered the bottom location of the ball (relative to the world as the ball spins) with the "OnCollisionEnter" or something similar? Or should I make another Ask Question post?

avatar image robertbu · Jun 19, 2013 at 04:41 PM 0
Share

OnCollsionEnter() is only true for a single frame, so a user would have to get real lucky to make it work, (or you'd have to change to GetButton("Jump") and allow the user to hold the button down). OnCollisionStay() would be better, but I'd be concerned the ti$$anonymous$$g window would be too narrow. If it were me, I'd first try a raycast:

 if (Input.GetButtonDown ("Jump") ) {
     RaycastHit hit;
     if (Physics.Raycast(transform.position, Vector3.down, out hit)) {
     if (Vector3.Distance(transform.position, hit.point) < someDistante)
         //Add force for jump code goes here
     }
 }
avatar image Shocking · Jun 19, 2013 at 05:30 PM 0
Share

Thank you VERY $$anonymous$$UCH! These 2 aspects have been bugging me like crazy the last week or so lmao.

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

15 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

Related Questions

Jumping in only the direction of the last key pressed 0 Answers

Hey Guys I have a problem with a single jump script. 1 Answer

Adding a jump feature help? 2 Answers

Advice needed on my first c# 2D jump script 1 Answer

can anyone help me to smooth the jump? 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