Collision Questions

Hi folks. I’ve just gotten collision detection working for the trains in my game. When a train realizes it has bumped into another train, it turns around. I’m not using any physics. I’ve got three questions for you all:

  1. In my testing, when two trains collide, they both turn around, as they should. But I’m worried I’ve just gotten lucky. Since each train detects the crash independently, is it possible that the first train detects the collision, turns and moves, and thus when the second train checks for a collision, it isn’t found, since the first train has already moved away? Or is the collision reported to both parties as soon as it registers?

  2. I’d like colliding trains to spawn a small crash/flying-debris animation. I don’t want both trains to spawn a crash, but the obvious solution would do just that. Is there a natural way to spawn just one? I realize I could index the trains and have the smaller-indexed train handle the crash animations, but I was hoping there might be a cleaner approach.

  3. My trains are basically empty game objects containing a health bar, a train script, and the model of the train. Currently the model has the rigidbody and collider on in, and a script whose sole purpose is to tell the main script that a collision has happened. Is this a reasonable way to handle things? It seems a little clunky, but I’m new to all this stuff.

Thanks very much in advance!

1- Collision events are sent to both colliders, thus you will not miss the collision.

2- Since both trains receive the collision event, you must use some trick to avoid a double explosion - something like the index you’ve mentioned. You could give different names to the trains, and compare them in the collision event:

function OnCollisionEnter(col: Collision){
  if (name <{ // the "lower" name handles the explosion
    var pos = col.contacts[0].point;
    Instantiate(explosion, pos, Quaternion.identity);

3- I think you hierarchy is ok - frequently we must child the model to an empty object to adjust axes and/or the pivot; the empty object becomes then the actual object, to which we attach the main components: scripts, collider, rigidbody etc.