How to disable/remove double Jumping.

Hi guys, completely new in unity. I’m asking how to remove double jumping. Do I have a mistake or my script is wrong? Or this code is influencing the other codes in my so that’s why it isn’t working(vice-versa)? Thanks for helping!!.

This is the part of my code that lets me jump. I also have other parts that let me move using w, a, s, d.

Please let me know if I can improve some of it and what’s the best code for that particular movement is.

If this code doesn’t have any mistakes, then it’s possible that the other parts of the script are influencing this part. Let me know if you want to check out the other parts of my script.

^— the link to the video that I watched to get most of the codes.

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

//Jf = Jump force
//Rb = RigidBody

public float Jf = 10f;
private Rigidbody Rb;
private bool Jumped;

    void Start()
    {
        Rb = GetComponent<Rigidbody>();
    }

void Update()
{
    if (Input.GetKeyDown("space"))
    {
        Jumped = true;
    }
}

void FixedUpdate()
{
    //Jump
    if (Jumped)
    {
        Rb.AddForce(Vector3.up * Jf, ForceMode.VelocityChange);
        Jumped = false;
    }
}
I really appreciate any solutions cause I've been stuck in this for a day ;-;

I would delay hhe jumped = false until you did a isGrounded check after you sent it al.oft.

Fire a raycast down to determine if you’re standing on the ground. Only jump if you are standing.

I think his code does not have a “isGrounded” check.

should I put the isGrounded under void Update() or I’ll make void OnCollisionStay() and put it there?

Can I ask? what is Physics.Raycast and how to do it since it’s my 1st time knowing it? Thanks!

hi guys, i have an update.
i added:

public bool isGrounded;

void OnCollisionStay()
{
isGrounded = true;
}

void FixedUpdate()
{
if(Jumped && isGrounded)
{
Rb.AddForce(Vector3.up * Jf, ForceMode.VelocityChange);
Jumped = false;
isGrounded = false;
}

it fixes the double jumping but can anyone pls give suggestions on how to improve the codes. and also when I land, my gameobject bounces. how to fix it? Thanks!!

https://docs.unity3d.com/ScriptReference/Physics.Raycast.html

You need to go through proper tutorials and read the docs isntead of trying to copy some code in hopes that it will work.

Physics material properties have bounciness parameter or something like that. Set bounciness to zero.

1 Like