Hello, I’m trying to instantiate two prefabs at a right angle to the where the collider hits a target.
Background: I’m making my own version of Asteroids in Unity and when a laser hits a asteroid and the asteroid splits, I want it to split to either side of the impact point.
For example, a laser hits an asteroid:
--> O
I would instantiate a prefab above and below the asteroid because that’s at 90 degrees to the left and right of the laser’s impact point:
x
--> O
x
public GameObject splitPrefab;
void OnCollisionEnter2D(Collision2D coll)
{
Quaternion randomRotation;
Vector3 newPosition;
for (int i = 0; i < 2; i++)
{
// The position of the circle
newPosition = gameObject.transform.position;
// the Collision2D will give us information about the arrow
if (i == 0)
{
// rotate left 90 degrees
}
else
{
// rotate right 90 degrees
}
Instantiate(splitPrefab, newPosition, Quaternion.identity);
}
}
I’m not sure what sort of math I need here to find the degree difference and then apply that to my newPosition with a distance for each prefab. The solution should work regardless of what direction the laser comes from; for example a laser could hit the same point on a asteroid but it could come directly towards the circle or be a glancing blow. I think in both situations it should be the same. The asteroid will likely be rotating and thus not stationary in that way.The distance between the asteroids center and the new prefab center would likely be a public variable that would depend on the game. This only needs to work in 2D.