- Home /
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!
Note : a game object with a health bar, a script, a model(mesh), a rigidbody, a collider, and another script is not 'basically an empty game object'.
Sorry, I just meant that not much is at the top of the hierarchy: the mesh, rigidbody, collider and other script are all components of a child.
Answer by aldonaletto · Apr 29, 2012 at 12:38 AM
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 < col.gameObject.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.
Your answer
Follow this Question
Related Questions
Destroying object when player walks over it 1 Answer
Ignore collision at high velocity. 1 Answer
Trouble with Physics.IgnoreCollision 0 Answers
Unwanted jittery behavior 2 Answers
Colliders in a wall jut out 0 Answers