i want my character to only be able to jump, when he is on the ground. However, my code only works like 75% of the time, sometimes just notjing happens when i press space. Here’s my script, but i dont know if the script is even the problem:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Spieler : MonoBehaviour
{
private Rigidbody Rigidbody;
public bool canJump = true;
private void OnCollisionEnter(Collision Collision)
{
if (Collision.gameObject.tag == "Ground")
{
canJump = true;
}
}
private void OnCollisionExit(Collision Collision)
{
if (Collision.gameObject.tag == "Ground")
{
canJump = false;
}
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
if (canJump == true)
{
Rigidbody.AddForce(new Vector3(0, 6, 0), ForceMode.Impulse);
}
}
}