- Home /
Using Collider.OnCollisionEnter(Collision) without attaching the script to the concerned GameObject
Hi :),
I have question about Collisions.
Let's say i have 2 GameObjects, Cube1 with a BoxCollider (Cube1Collider) and a rigidbody and Cube2 with a BoxCollider (Cube2Collider) and a rigidbody.
Normally i attach this script on Cube1 to get information about the collision occuring between his Collider and others Colliders.
code (javascript) : function OnCollisionEnter(CollisionInfo:Collision) {
var CollidedCollider = CollisionInfo.gameObject.name; var Point:Vector3 = CollisionInfo.contacts[0].point; var Normal:Vector3 = CollisionInfo.contacts[0].normal; Debug.Log(CollisionInfo.contacts[0].thisCollider.name+" hit "+CollisionInfo.contacts[0].otherCollider.name); Debug.Log("CollidedCollider : " + CollidedCollider); Debug.Log("Point : " + Point); Debug.Log("Normal : " + Normal);
}
This is working correctly, but i don't want to manage my program this way ! I don't want to attach many scripts to many GameObject and i want to be able to test the collision between Cube1 and Cube2 in code.
For example i want to attach a main script called "Program" to an Empty GameObject and be able to test collisions between 2 GameObjects in code. But i have not managed to do this so far...
It would look like this : code javascript : function Cube1Collider.OnCollisionEnter(CollisionInfo:Collision) {
var CollidedCollider = CollisionInfo.gameObject.name; var Point:Vector3 = CollisionInfo.contacts[0].point; var Normal:Vector3 = CollisionInfo.contacts[0].normal; Debug.Log(CollisionInfo.contacts[0].thisCollider.name+" hit "+CollisionInfo.contacts[0].otherCollider.name); Debug.Log("CollidedCollider : " + CollidedCollider); Debug.Log("Point : " + Point); Debug.Log("Normal : " + Normal);
}
But it doesn't work...
Do you have any idea how to achieve this ?
Thanks
Answer by superpig · Apr 24, 2011 at 12:23 PM
You have to attach some kind of script to the gameObjects that participate in the collision. There is nothing built into Unity that allows objects that aren't participating in a collision to find out about them.
If you really wanted to process the collision in a central object, you could have the collision handler on each gameObject be a really simple script that just passed the data to a central object. I think this would be a bad idea though; information about the way different objects react to different collisions should be spread across the objects in question, because it reduces coupling and makes the system easier to change and extend.
Answer by ranko · Apr 24, 2011 at 01:04 PM
Thanks for the answer. Yes that's what i have found and i am not accustomed to attach many scripts to many entities.
Moreover i create all my entities (GameObject) in code and i am able to add colliders and rigidbodies, but i am not able to use OnCollisionEnter() because i can't specify the entity1 and entity2 to check if there is a collision...
So i guess the procedural creation of a level is not compatible with OnCollisionEnter() and therefore i will have to code my own collision system with Raycast and Spherecast...
What do you mean by : "If you really wanted to process the collision in a central object, you could have the collision handler on each gameObject be a really simple script that just passed the data to a central object."
Is this method compatible with procedural creation of entities ?
Thanks,