- Home /
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 :)
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.
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?
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
}
}
Thank you VERY $$anonymous$$UCH! These 2 aspects have been bugging me like crazy the last week or so lmao.
Your answer
![](https://koobas.hobune.stream/wayback/20220613104452im_/https://answers.unity.com/themes/thub/images/avi.jpg)