I’m trying to make my character jump if i press space while on the ground. To check if i’m on the ground, i have a small box, with the width of my character lying under the player’s feet. i want to check if the box is colliding with an object to determine whether or not the player is touching the ground.
From what i can tell from research, i need to be using the OnCollisionStay function, so i’ve set it up here and attached it to my box.
public GameObject player;
private Rigidbody rb;
void Start() {
rb = player.gameObject.GetComponent<Rigidbody> ();
}
void OnTriggerStay(){
if (Input.GetKeyDown ("Space")) {
rb.AddForce(0,500,0);
}
}
This code doesn’t work, and i’m not sure why. Can someone point out what i might be doing wrong? My logic is that as long as the the box is colliding with something, the code will check if i’m pressing space and shoot my character upwards. I’ve attached the player gameobject to the player variable via the inspector.
Note: i’ve tried using raycasts but it didn’t work so well on ledges since the player’s center needs to be touching the ground.