So far, I've been able to move a ball on a 3-dimensional plane using the arrow keys. But I'm not sure how I should make the ball jump. This is the code I have added to the ball to make it move on the plane:
public class PlayerController : MonoBehaviour
public float speed;
private Rigidbody rb;
void Start ()
rb = GetComponent<Rigidbody>();
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3 (moveHorizontal,0.0f,moveVertical );
rb.AddForce(movement * speed);
I want to know what I must add to this code in order to make the ball jump.
Answer by _Sewq90
Apr 26, 2015 at 09:25 PM
I do this for my character to move
rb.AddForce(Vector3.up * jumpHeight);
just make a public/private variable named jumpHeight.
reading the function overloads is very useful, thanks for this answer!
Answer by lordlycastle
Apr 21, 2015 at 01:24 PM
If it’s simplicity you’re looking for, this should work:
rb.AddForce(new Vector3(0, 10, 0), ForceMode.Impulse);
This will make the ball jump when you press space. Although I would recommend, you look at the provided standard assets script for controlling Ball; here they are.
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
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.
Answers and Comments
4 People are following this question.
Distribute terrain in zones
How can I edit this script to double jump?
Multiple Cars not working
Get Y value of plane at position X,Z