player can jump infinitely?

for some reason my player can jump infinitely when you continually press space. I have made a grounded variable but it doesn’t work. any ideas? :eyes:

var walkAceleration: float = 5.0f;
var walkAccelerationAirRatio : float = 0.1;
var walkDeacceleration : float = 5;
@HideInInspector
var cameraObject : GameObject;
var rb : Rigidbody;
@HideInInspector
var horizontalMovement : Vector2;
var jumpVelocity : float = 20;
@HideInInspector
var grounded : boolean = false;
var maxSlope : float = 60;
var crouchRatio : float = 0.3;
var transitionToCrouchSec : float = 0.2;
var crouchingVelocity : float;
var currentCrouchRatio : float = 1;
var originalLocalScaleY : float;
var crouchLocalScaleY : float;
var collisionDetectionSphere : GameObject;

function Awake()
{
    currentCrouchRatio = 1;
    originalLocalScaleY = transform.localScale.y;
    crouchLocalScaleY = transform.localScale.y * crouchRatio;
}


function Start()
{
   rb = GetComponent.<Rigidbody>();
}
function Update ()
{
  
   transform.localScale.y = Mathf.Lerp(crouchLocalScaleY, originalLocalScaleY, currentCrouchRatio);
   if (Input.GetButton("Crouch"))
           currentCrouchRatio = Mathf.SmoothDamp(currentCrouchRatio, 0, crouchingVelocity, transitionToCrouchSec);
   if (Input.GetButton("Crouch") == false && collisionDetectionSphere.GetComponent(CollisionDetectionSphereScript).collisionDetected == false)
           currentCrouchRatio = Mathf.SmoothDamp(currentCrouchRatio, 1, crouchingVelocity, transitionToCrouchSec);
  
  
   GetComponent.<Rigidbody>().velocity.z = Mathf.Clamp(GetComponent.<Rigidbody>().velocity.z, -20, 20);
   GetComponent.<Rigidbody>().velocity.x = Mathf.Clamp(GetComponent.<Rigidbody>().velocity.x, -20, 20);  //Change the -10 and the 10 to alter movement speed

   if (Input.GetAxis("Horizontal") == 0 && Input.GetAxis("Vertical") == 0 && grounded){
          GetComponent.<Rigidbody>().velocity.x /= walkDeacceleration;
          GetComponent.<Rigidbody>().velocity.z /= walkDeacceleration;}
  
  
   transform.rotation = Quaternion.Euler(0, cameraObject.GetComponent(MouseLookScript).currentYRotation, 0);
  
   if (grounded)
         rb.AddRelativeForce(Input.GetAxis("Horizontal")* walkAceleration, 0, Input.GetAxis("Vertical")* walkAceleration);
   else      
         rb.AddRelativeForce(Input.GetAxis("Horizontal")* walkAceleration * walkAccelerationAirRatio, 0, Input.GetAxis("Vertical")* walkAceleration * walkAccelerationAirRatio);
  
   if (Input.GetButtonDown("Jump"))
           GetComponent.<Rigidbody>().AddForce(0,jumpVelocity,0);
}

function OnCollisionStay (collision : Collision)
{
    for (var contact : ContactPoint in collision.contacts)
    {
        if (Vector3.Angle(contact.normal, Vector3.up) < maxSlope)
            grounded = true;
       
    }
}
function OnCollisionExit()
{
    grounded = false;
}

This doesn’t check if the character is grounded, or for any conditions other than if Jump is pressed. Add proper conditions to prevent it from being able to jump whenever the key is pressed.

if (Input.GetButtonDown("Jump"))
           GetComponent.<Rigidbody>().AddForce(0,jumpVelocity,0);

I changed it to

if (Input.GetButtonDown("Jump") && grounded == false)
           GetComponent.<Rigidbody>().AddForce(0,jumpVelocity,0);

but now i cant jump?

You’re checking if grounded is false, don’t you want it to be true?

A) Am I touching the ground?
B) Yes I am, therefore I can jump!

yeah whoops, all works but just as a side note, do you know how I could set a maximum walk speed

Unrelated but your physics stuff should be in FixedUpdate (), rather than Update ().
https://unity3d.com/learn/tutorials/modules/beginner/scripting/update-and-fixedupdate

Assuming your character can walk on the x and z axes, I have no idea. It’ll be a little bit more tricky than just comparing velocity values to predefined max values and then adjust.

I guess you could store your position every frame, then compare your current position to your last position and check what the distance is between them. If the distance is > a predefined max distance, it means you’re traveling too fast and you should reduce the velocity. That sounds really sloppy though, I’m sure others have much better ways of doing this.

1 Like

You could try checking the normalised velocity * speed & if it is greater than the max speed int reset it.