- Home /
Sphere can jump, but doesn't stop.
I have a sphere (that I'm using like the character) it moves around with this code:
public float speed;
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
rigidbody.AddForce(movement * speed * Time.deltaTime);
}
and that work perfectly. But I wanted to be able to jump. I tried out a couple of jumping scripts I found online, but had the same problem with both: Once I pressed the spacebar my sphere would jump and not stop. It just kept jumping up and down. I think this is because it didn't know when it had landed, maybe?
Could someone give me some C# code to add to my script that will make it jump once?
Thanks!
Answer by NickP_2 · Feb 05, 2014 at 10:43 PM
in thispost you can find out if the player is grounded (touching the ground) or not. Only allow the player to jump if touching the ground is true.
The charactercontroller has a build in function for this, which is why it's so commonly used. A rigidbody is mostly used for interaction objects, but it is possible :)
Well, it jumps, lands, and jumps again etc. So it just doesn't stop, is the problem. I'm not using the character controller because it's a sphere, and I needed it to roll.
Your answer
Follow this Question
Related Questions
Triple Jump C# Script 1 Answer
Cant Jump ?c# please 2 Answers
Multiple jump C# Script. 0 Answers
Increase jumpheight while holding down a key 3 Answers
Jumping in only the direction of the last key pressed 0 Answers