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.
var CollidedCollider = CollisionInfo.gameObject.name; var Point:Vector3 = CollisionInfo.contacts.point; var Normal:Vector3 = CollisionInfo.contacts.normal; Debug.Log(CollisionInfo.contacts.thisCollider.name+" hit "+CollisionInfo.contacts.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...
But it doesn't work...
Do you have any idea how to achieve this ?
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 ?
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
The best place to ask and answer questions about development with Unity.
To help users navigate the site we have posted a site navigation guide.
If you are a new user to Unity Answers, check out our FAQ for more information.
Make sure to check out our Knowledge Base for commonly asked Unity questions.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
Answers and Comments
No one has followed this question yet.
GO.AddComponent just adds an empty Script
How to add new instance of a script to a game object?
Config file for every object
How to delay lines of code.
Problem accessing script