0
\$\begingroup\$

I am working on a 2d platformer-like game in Unity. My problem is that when my player character jumps and lands back on the ground, there is a slight delay at times when transitioning to the player idle animation. This is not due to exit time. Instead, upon researching this topic, I found that my real problem lies in the Unity physics loop(FixedUpdate).

FixedUpdate, as the name implies, is called a fixed amount of times, not every frame. My animator relies on a Jumping boolean to tell it whether or not to be in the jumping animation. In my code, the Jumping boolean is set to false when the player lands on the ground. This is handled by OnCollisionEnter2D, which runs on FixedUpdate.

This is were I think my problem lies:

private void OnCollisionEnter2D(Collision2D collision)
{
    if (collision.gameObject.CompareTag("Ground"))
    {
        _isOnGround = true;
        _animator.SetBool("Jumping", false);
    }
}

I need to know whether or not the player is colliding with the ground, So I setup a tag. I need to find a way to constantly check whether or not I am colliding with the ground, So that I do not see an animation transition delay.

Any Ideas of how I could accomplish this?

\$\endgroup\$
0
\$\begingroup\$

You should check out Physics2D.OverlapArea. It takes a rectangle and checks if it is overlapping with another sprite. Here is some code I have been using that works well.

//Corners of the rectangle to check
 public var top_left : Transform;
 public var bottom_right : Transform;
 public var ground_layers : LayerMask;
 var grounded : bool;

 function FixedUpdate(){
     grounded = Physics2D.OverlapArea(top_left.position, bottom_right.position, 
   ground_layers);    
  }
\$\endgroup\$
0
\$\begingroup\$

I found that changing the rigidbody collision detection type to continuous helped remedy the delay. Although it still is there to an extent, this is the best solution I could find for the time being.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.