I am unsure how to make it so I can jump only when my character is on the ground I assume it has to do with OnCollisionEnter and OnCollisionExit but am unsure what to put in.
Here is my Script
using UnityEngine;
using System.Collections;
public class Runner : MonoBehaviour
{
public float Speed =1;
public Vector3 JumpHeight = new Vector3();
public bool IsOnGround = true;
public float DeathHeight =-10;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update ()
{
if (!IsOnGround)
{
rigidbody.AddForce (new Vector3 (Speed, 0, 0), ForceMode.Acceleration);
}
if (gameObject.transform.position.y < DeathHeight)
{
rigidbody.velocity = new Vector3();
gameObject.transform.position = new Vector3(1,1,0);
}
if (Input.GetMouseButtonDown (0))
{
IsOnGround = false;
rigidbody.AddForce(JumpHeight,ForceMode.Impulse);
}
}
void OnCollisionEnter()
{
IsOnGround = true;
}
void OnCollisionStay()
{
IsOnGround = true;
}
void OnCollisionExit()
{
IsOnGround = false;
}
I am unsure what to put in the script to make it so the jump only works when I am on an object. (This is for an endless runner if that helps at all)