OnCollisionStay going one more time after i jump

im using OnCollisionStay to check if the player touched the ground, and i want to make it so the player can grind if they jumped, the problem is it goes into OnCollisionStay and disables the variable tracking that you jumped up

it’s hard to explain so heres some code i used to mess with it:

    function FixedUpdate ()
    {
    
            // Jump
    		if (Input.GetMouseButtonDown(0))
    		{
    			rigidbody.velocity = Vector3(velocity.x, CalculateJumpVerticalSpeed(), velocity.z);
    			justJumped = true;
    		}
    }
    
    function OnCollisionStay () {
    	justJumped = false;
    }
    
    function OnTriggerStay (hit : Collider) {
    
    	//GRIND
    	if(hit.tag == "rail")
    	{
    		//so player jumped to grind
    		if(justJumped)
    		{
                //grind code
            }
        }

    }

if i debug.log where i turn on/off justJumped, OnCollisionStay runs one more time before i am completely in the air

is this because of the order that these functions go? I dont know how to fix this…

thanks

oh that's a good idea, then it would prolly be more reliable too maybe i will just do that instead

if you want you can convert your comment to an answer as it totally solved my problems haha, thanks i have this on the top of my update before anything else and everything is workin fine //DETERMINE IF ON GROUND var rayHit : RaycastHit; if (Physics.Raycast (transform.position, -Vector3.up, rayHit, 1.25)) { grounded = true; }else { grounded = false; }

Done. Glad it helped.

And just FYI, since Raycast returns a bool, that can be done in one line, and unless you need the hit info it's faster not to pass it in.

oh cool thanks again, i thought you had to have the hit for some reason

1 Answer

1

Instead of using OnCollision handlers to determine if you’re touching the ground you might consider using a raycast downwards.