Is there a better way of doing this

Im trying to get rid of double/triple/you get the point jumping so I went with

if(Input.GetKey(KeyCode.Space))
        {  
            if (jump = true){
                y += transform.position.y * Time.deltaTime * jumpsensitivity;
                transform.position = new Vector3(x, y, z);}
                jumped = true;
                wait 10f
                jumped = false;
        }
OnCollisionEnter(){
            wait 1f
            jump = true
        }
        OnCollisionExit(){
            if(jumped = false){
                jump = false
                wait 1f
                jump = false
            }

Is there a better way to do this? Don’t comment on the errors fixing them rn just curious of other methods

No, not really at all.

Steps to success:

  • read intent to jump in Update()
  • set a boolean if want jump
  • observe and process jump boolean in FixedUpdate(), clearing it when you do.

Example:

Code located in:

proximity_buttons is presently hosted at these locations:

https://bitbucket.org/kurtdekker/proximity_buttons

https://github.com/kurtdekker/proximity_buttons

ignore the wait lmao fixing it

You could have problems using OnCollisionEnter and OnCollisionExit to keep track of whether you’re on the ground. Your character could walk from one surface onto another and the OnCollisionExit would trigger resulting in your jump bool being set to false, despite still being on a surface.

Also, OnCollisionEnter can be triggered if your character is touching something with their head.

It’s generally best to do a SphereCast or a Raycast to check if there’s something immediately below your player’s feet before allowing them to jump.

Or if you really want to use the collision events then use OnCollisionStay. You could even check for the jump key from OnCollisionStay and then you won’t need a jump bool and OnCollisionExit.

    void OnCollisionStay(Collision c)
    {
        if (Input.GetButton("Jump"))
            rb.AddForce(Vector3.up*6,ForceMode.Impulse);
    }

This is a good idea but what happens if theres a piercing projectile that hits you mid air? this would allow for a double jump with proper timing.(edit: am I missing something?)

Layers and LayerMasks are used to separate collision concerns / types:

The difference between Layers vs LayerMasks:

https://discussions.unity.com/t/802897/2

“There are 10 types of people in this world: those who understand binary, and those who don’t.”