Following a tutorial I was able to make a simple ball jump. However the ball jumps only on y direction and only when on the ground. My question is why the ball is not jumping when it’s moving or why doesn’t jump in the direction of movement (left or right). Here is how I coded:
using UnityEngine;
using System.Collections;
public class BallControl : MonoBehaviour {
// Use this for initialization
public float rotationSpeed = 100;
public float jumpHeight = 8;
private bool isFalling = false;
// Update is called once per frame
void Update ()
{
//-----------Handle ball rotation
float rotation = Input.GetAxis("Horizontal") * rotationSpeed;
rotation *= Time.deltaTime;
GetComponent<Rigidbody>().AddRelativeTorque(Vector3.back * rotation);
if(Input.GetKeyDown(KeyCode.Space) && isFalling == false)
{
GetComponent<Rigidbody>().velocity = new Vector3(0, jumpHeight, 0);
}
isFalling = true;
}
void OnCollisionStay()
{
isFalling = false;
}
}