Problem with collisions between instantiated prefabs.

Hello All,

I have a collision script attached to all my instantiated prefabs which checks OnCollisionStay(), I have no problem detecting collisions and acting upon them, however I do have an issue with making the collided prefabs do what I need them to do. I will give an example.

I instantiate 20 enemy game objects which when close enough will follow my player, if my player stops the enemy prefabs will approach and stop when next to the player. If however the enemy prefabs approach in a line (as in directly behind each other) they queue up one after another and never get to the player. My simple solution to this (so I thought) would be to check for a collision between the enemy prefabs as they are pushing up against each other and then move them either left or right until they can move past the prefab in front and get to the player. Herein lies my problem, when I check the collision between the prefabs I tell the rigidbody of the mesh that has collided to move to the left and the rigidbody of the mesh from the collider to move to the right. However, since the OnCollisionStay() is being called on both objects the process cancels itself out.

I’m not really looking for any help with Code as I have this covered but if anybody has any ideas of how to overcome this little conundrum I would be very happy to hear your thoughts. I’ve spent a bit of time on this now and my head is beginning to hurt.

I am unable to post any code at the moment as I’m not at home, but if you want the collision checking bit I would be more than happy to post later when I get back.

Many thanks for reading and I look forward to your replies.

It’s wise to avoid calling your prefab instances “prefabs” in conversation. Call them “instances”.

As soon as this situation presented itself in a project of mine, I went a little crazy with the complexity of the solution. Because this is basically the simplest possible example of this particular issue, I’m sure there’s an easier solution. But hey, it’s a fine opportunity to learn about managerial classes.

I would need two-or-more objects to make a single group decision quite frequently, so I created a CombatDirector class (a singleton) to handle all such logic.

Here’s a condensed example of how the director singleton might work:

public struct EnemyPair {
  public Enemy a;
  public Enemy b;

// Both of your colliding enemy objects call this simultaneously OnCollisionEnter.
// They pass their own script, and the "other" object's script. (this, other.enemy)
// In a more complex example, this method could receive generic type
// arguments, and a delegate to inform "what should be done" with them.
// That way you could trigger different events based on the context.  
// This is a powerful concept, but a bit much for an example.
public static void AddEventRequest( Enemy a, Enemy b ) {
  // here, you check whether any pending event is already represented
  // and reject it.  
  // foreach EnemyPair in pendingEvents if (a==a AND b==b OR a==b AND b==a) return;
  pendingEvents.Add( EnemyPair(a, b) );

void Update() {
  if (pendingEvents.Count>0) {

Also, if you haven’t already seen this, it’s THE resource to show anyone who’s just learning about steering behaviors: Steering Behaviors For Autonomous Characters