# How do I stop infinite jumping?

So I’m a beginner that is dealing with an infinite jumping problem, and I’ve looked through some forums before this, and I still can’t figure it out.
Here’s my code:

``````using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour{

public float speed = 10f;
CameraMovement cameraPlace;
bool touchingFloor;
public Collider floorCol;

void Start(){
cameraPlace = FindObjectOfType<CameraMovement>();
cameraPlace.rotation = transform.rotation;
touchingFloor = true;
}

void Update(){

Vector3 input = new Vector3 (Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical"));
Vector3 direction = input.normalized;
Vector3 velocity = direction * speed;
Vector3 moveAmount = velocity * Time.deltaTime;

transform.Translate(moveAmount);

float yValue= cameraPlace.cameraRot.y;
cameraPlace.cameraRot = new Vector3(0f, yValue, 0f);
transform.eulerAngles = cameraPlace.cameraRot;
cameraPlace.rotation = Quaternion.Euler(cameraPlace.cameraRot);

if(touchingFloor = true && Input.GetKeyDown(KeyCode.Space)){
Vector3 v = GetComponent<Rigidbody>().velocity;
v.y = 4.2f;
GetComponent<Rigidbody>().velocity = v;
touchingFloor = false;
}

OnColliderEnter(floorCol);

}

void OnColliderEnter(Collider floorCol){
if (floorCol.gameObject.tag == "floor"){
touchingFloor = true;
}

}
}
``````

Hey, so it may be helpful to provide some more context like does the rigidbody component have isKinematic checked? also maybe describe the “infinite jumping”, is it one jump after the other or one infinitely long jump?

There are some candidate issues in your code, that may not be the cause of your bug, but you may want to handle as they will easily be a bug at some point.

Look into the difference between Update and FixedUpdate, and more generally, the seperation between the physics callback functions (FixedUpdate, OnCollisionEnter/Exit, OnTriggerEnter/Exit). When it comes to moving an object with a rigidbody, you should choose one of the following:

1. mark it as kinematic (via code or inspector) and then move it freely using its transform component only. Dont use AddForce() or set it’s velocity. The movement should be done in the Update function.

2. dont mark it as kinematic, but dont move it via its transform. Only move it using the rigidbody and physics engine. even here you shuld be mindful of directly setting its velocity.

you are calling OnColliderEnter() every frame (every Update()). OnColliderEnter is a callback that is triggerred during the physics step. The unity engine triggers this function on its own when it detects a collision and you generally never call it explicily from your own code (although there is nothing technically wrong with doing so).
basically, your variable ‘touchingFloor’ is always equal to true, as the only time you set it to false, you reset it to true immediately in the next line.

It looks like the issue may be with the way you are checking if the player is touching the floor. In the Update function, you are setting the touchingFloor variable to the result of the OnColliderEnter method, which will always be true because it is a method and not a boolean value.

To fix this, you can update the Update function to check the touchingFloor variable directly instead of calling the OnColliderEnter method. You can also update the OnColliderEnter method to accept a Collision parameter instead of a Collider parameter, and use the collider property of the Collision object to check if the player is colliding with an object with the “floor” tag.

Here’s how you can modify the Update and OnColliderEnter functions to fix the infinite jumping issue:

``````void Update()
{
// ... other code omitted for brevity ...

if(touchingFloor == true && Input.GetKeyDown(KeyCode.Space))
{
Vector3 v = GetComponent<Rigidbody>().velocity;
v.y = 4.2f;
GetComponent<Rigidbody>().velocity = v;
touchingFloor = false;
}
}

void OnCollisionEnter(Collision collision)
{
if (collision.collider.gameObject.tag == "floor")
{
touchingFloor = true;
}
}
``````

With these changes, the player should only be able to jump when they are touching the floor, and should not be able to jump indefinitely.

I hope this helps! Let me know if you have any other questions.

Thanks to both of you these helped me out a lot. As I said, I am a beginner so pretty much all help is useful in some way.

1 Like