I have an objects they are randomly instantiated, when they moving by assigned axis they may collide each other, is there a way to know in which axis they are collided? To make question more clear: 5 game objects moving around and some of them collided in x, y, z axis, so I need to detect which game objects and in which x, y, z axis they has been collided. Thanks for any answers!
there are several methods, the simplest of which is probably just testing the angle (you can figure this out by using Vector3.Angle() with transform.forward/up ect.
what i will show is a method that gets the vector from the position to the contact point and then sees which of the axes are the largest and if its negative or positive.
void OnCollisionEnter(Collision col)
{
float Vector3 directionVector = col.point - transform.position;
direction = direction.normalized
if(Mathf.Abs(direction.x) > Mathf.Abs(direction.z))
{
if(Mathf.Abs(direction.x) > Mathf.Abs(direction.y))
{
if(direction.x > 0)
//collision is came from right
else
//collision came from left
}
else
{
if(direction.y > 0)
//collision is came from top
else
//collision came from down
}
}
else
{
if(Mathf.Abs(direction.z) > Mathf.Abs(direction.y))
{
if(direction.z > 0)
//collision is came from front
else
//collision came from back
}
else
{
if(direction.y > 0)
//collision is came from top
else
//collision came from down
}
}
}