Hi to all
I wrote the simple following code to check if player is grounded or not (“groundLayer” is a layer I passed to script):
public bool isGrounded()
{
if (capsuleCollider.IsTouchingLayers(groundLayer.value))
{
isJumping = false;
return true;
}
return false;
}
So I Debug.Log if it is grounded every Update, but sometimes it return false even if collider2d is touching collider with “Floor” layer. It happens randomly and for a fraction of a second, I noticed it because sometime animation I created for the player seemed weird.
Any idea of why this weird behaviour?
Thanks in advance
From the documentation: “It is important to understand that checking if colliders are touching or not is performed against the last physics system update i.e. the state of touching colliders at that time. If you have just added a new Collider2D or have moved a Collider2D but a physics update has not yet taken place then the colliders will not be shown as touching. The touching state is identical to that indicated by the physics collision or trigger callbacks.”
The input parameter is “layermask”, nor layer. Try,
Physics2D.isTouchingLayers(capsuleCollider, Layermask.GetLayer(“layerName”)).
I have not tested with collider2D.isTouchingLayers method but that should work as well.
@alex174
I noticed that this happens randomly when player collider is walking over two adjacent platform colliders.
If ground is composed by only one collider problem that I described in question doesn’t happens.
However I would build platforms of various sizes, so it’s not so comfortable to create an object made by more platform sprites and create a single collider covering all of them.
The desired behaviour is to create adjacent ground objects all of them with its own collider and connect them to make one single platform.
I hope it’s explained well, it’s not easy… Hope you can help me with this problem.
Found which was the problem:
problem wasn’t method to check if player is grounded, but when player touches adjacent colliders. Now I’m using Tilemap with TilemapCollider2D and Composite Collider2D (with other problems) and when touching composite player is grounded. Hope this helps you in future.