Hello, How can I detect which side of gameobject is my player touching ??
i want to detect if i am on top or on the sides.
I am using tilemap collider and composite collider.
Hello, How can I detect which side of gameobject is my player touching ??
i want to detect if i am on top or on the sides.
I am using tilemap collider and composite collider.
In your:
private void OnCollisionEnter2D(Collision2D other)
You get the other object of type Collision2D. There you’ll get the contact points:
foreach (var contact in other.contacts)
{
Debug.DrawRay(contact.point, contact.normal, Color.red, 10f);
}
This way you can detect, which way your player hit wall.
However, I have not used this with Tilemap so maybe someone else knows more about this (if it works or not) but I guess it should work.
Personally, I would be raycasting in my character controller in a 2d game for collisions and use that to work out where it is making contact.
So what I would suggest is raycast in the direction the player is moving on the x and y and that will tell you if it is hitting up, down, left or right.
would that work of the object is rotating
Gonna try it
Yes, try it, it works like this (btw, seems to work also with TileMaps, but this was with colliders!):

Thanks, it worked