- Home /
How to make a ball jump?
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:
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour
{
public float speed;
private Rigidbody rb;
void Start ()
{
rb = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
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
if (Input.GetKey("whatever"))
{
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:
if (Input.GetKeyDown(KeyCode.Space)
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.
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Jumping Ball 1 Answer
3d Animated Jump Script 0 Answers
3D coyote time 1 Answer