I'm trying to determine the collision-direction between two objects. I'll be using this for various things such as creating blood-entities in a specific direction proportional to where the other entity is hitting an entity. What's the best/most efficient way to do this? I've been doing this but it seems unstable/not to be working.
Assuming both entities are center-pivoted, I use trigonometry to find the angle like; atan(b/a). For some reason the output angle seems to be pretty random.
Here's the method I made:
protected void checkCollisionDirection(Collision2D target)
{
Vector3 positionDifference = target.gameObject.transform.position - gameObject.transform.position;
int quadrant = 0;
float x = positionDifference.x;
float y = positionDifference.y;
float degrees = Mathf.Atan (y / x) * Mathf.Rad2Deg;
if (positionDifference.x > 0 && positionDifference.y > 0)
{
quadrant = 1;
}
else if (positionDifference.x < 0 && positionDifference.y > 0)
{
quadrant = 2;
degrees += 180;
}
else if (positionDifference.x < 0 && positionDifference.y < 0)
{
quadrant = 3;
degrees += 180;
}
else if (positionDifference.x > 0 && positionDifference.y < 0)
{
quadrant = 4;
degrees += 360;
}
hitDirectionDegrees = degrees;
}
I'm open to any suggestions that can find an angle between two entities.