Spawn objects with velocity based on collision of two other objects.

Hello, I’m new here so I am sorry if this has been asked before. I tried searching for it but I can’t seem to find the right words for it.

Basically in Unity2D, I am trying to make it so instances of a new object(s) are spawned with a velocity or force in a direction depending on the collision or collision trigger between two objects, with their angle of movement determined by where the original object was hit.

For example: say one object (player) is moving up and hits the left side of another object (rock), the collision would spawn new objects (smaller rocks) moving right.

Would someone have an idea for how I would go about programming this?

If it’s sufficiently accurate to use object origins (and / or raycasting) for your calculations, that’s the cleanest way. Otherwise, you’ll have to play with Contact Points Unity - Scripting API: Collision.contacts, maybe averaging a selection of them to find the “best” point of contact. That’s almost certainly overkill, though.

Ideally, all this should be done using the normal of the “best” collision, where the newly spawned object’s initial velocity is informed by the collision normal. If you use raycasting, for instance, you can get your normal from the RaycastHit object. Unity - Scripting API: Physics.Raycast