How can I detect what direction a collision has occurred from?
When an enemy or a projectile collides with my player, I need to know what side the player was hit from. I am working in 2D, so I don’t need to know more than if it came from the left or right side, that is, positive or negative x.
I’m reading through the documentation, and collisions carry a “contacts” element that has information about where the collision occurred. But I can’t find how to access it.
The documentation just says:
So I looked up ContactPoint2D, and it has variable called point, which is a vector2 with the location in world.
But I don’t know how to retrieve this data.
I’ve tried
void OnCollisionEnter2D (Collision2D col)
{
//col.contacts.point.x
//col.contacts.x
//col.contacts.ContactPoint2D.point.x
}
but none of those return as something that contacts has a definition for. The documentation does not explain to me what I can get from the contacts.
My plan is to compare that collision point to the player’s location, like this:
if ( (world location of collision in x) < col.gameObject.transform.position.x)
Debug.Log("Hit from Left");
which I believe will work, but I don’t know how to retrieve that collision location properly.