- Home /
Problem with jumping
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);
}
}
}
Answer by Ercova · Aug 18, 2021 at 08:08 PM
I recommend you to use character controller. It will be more convenient when jumping and moving. But if you still want to try this, i would recommend you to ensure that u collide with the floor properly when land on it.
Answer by THUNDER_WOLF10 · Aug 18, 2021 at 08:59 PM
try changing from "OnCollisionEnter" ->"OnCollisionStay"
Your answer
Follow this Question
Related Questions
Ground detection (3D) 1 Answer
Multiple Cars not working 1 Answer
a better movement code C# 3 Answers
How to move while jumping? 1 Answer
Distribute terrain in zones 3 Answers