Hoping this is pretty straightforward
The following script is intended to have two objects collide and check for the correct collider type before destroying them both and replacing them with a new gameobject:
var newObject:GameObject;
function OnCollisionEnter (collision : Collision)
{
var contact = collision.contacts[0];
if(contact.otherCollider.name == "rightObject")
{
Destroy(contact.otherCollider.gameObject);
Destroy(contact.thisCollider.gameObject);
var newVelocity:Vector3 = (transform.rigidbody.velocity + contact.otherCollider.attachedRigidbody.velocity)/2;
var newPosition:Vector3 = contact.point;
var newObject2:GameObject = Instantiate(newObject, newPosition, Quaternion.identity);
newObject2.transform.rigidbody.velocity = newVelocity;
}
}
As one might expect, the code executes oce on each of the objects, resulting in two new objects instantiated instead of one… I was hoping that either:
- The object that moved that frame (causing the collision) could be considered the “collider” and the receiver the “collidee” and that I could execute the script for only the collider, or:
- I had hoped that by destroying both objects as early as possible, that only the first one processed would run the script (turns out to not work that way… both objects process it)
Is there a simple way to force the script to only be executed once when these kinds of symmetrical collisions occur?
Regards!
Tz.