My character can keep jumping without touching the ground, and they just keep going up.

Here is the code. I just want to make it so that the player can’t just keep jumping up and up.

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

public class Jump : MonoBehaviour
{
public CharacterController cc;
public float gravity = -9.81f;
public float gravityScale = 1;
public float jumpHeight = 4;
public float groundDistance = 0.4f;

Vector3 velocity;

public Transform groundCheck;

public LayerMask groundMask;

bool isGrounded;

void Update()
{

isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);

if(isGrounded && velocity.y < 0)
{
velocity.y = -2f;
}

if (Input.GetKeyDown(KeyCode.Space))
{
velocity.y = Mathf.Sqrt(jumpHeight * -2f * (gravity * gravityScale));
}
velocity.y += gravity * gravityScale * Time.deltaTime;
MovePlayer();
}
void MovePlayer()
{
cc.Move(new Vector3(0, velocity.y, 0) * Time.deltaTime);
}

}

If isGrounded is false, wouldn’t you want space to not do anything?

And use code tags, they make reading your code so much easier.

1 Like

how would I go about makin space do nothing? also with the code tags, sorry about that, that is just how my brain has to have the code, so i don’t mess up

The point of mentioning it was not to know why you didn’t use them but to ask you to use them i.e. edit your post to use them.

if (Input.GetKeyDown(KeyCode.Space))
{
if (isGrounded)
{
velocity.y = Mathf.Sqrt(jumpHeight * -2f * (gravity * gravityScale));
}

}

When you press space, add an if isGrounded to make sure that you’re grounded.

I managed to figure it out through hours of trial and error, but thanks for the help.