- Home /
Checking "if collision" from a script?
I try to write an RTS game. For placing buildings I need to check if they collide with other buildings.
I tried to use: OnTriggerEnter, OnCollisionEnter ...with different sets of an GameObject like: isTrigger, isKinematic, RigidBody...
When I had meshCollider and rigidbody the buildings on collision where just jumping from each other, or they just slope down from a hill.
The OnTriggerEnter and other events are checked when Im out of a method and there is one loop in Update(). And they dont return much.
The best way for me would be to have a method that returns true or false on collision. I would like to check it after I place the building too close to other object. Objects are in different sizes and shapes, so I dont know if checking some kind of radius will help.
I dont know why Ive got some problems. There is a nice table "Collision action matrix" taht shows different combinations. But I cannot run OnTriggerEnter, OnCollisionEnter, OnTriggerStay, OnCollisionStay if the object has no rigidbody or it is$$anonymous$$inematic.
I tried for me the best: to have one building with rigidbody , and without mesh collider to collide with already egzisted one which has mesh collider and without rigidbody. According to the table they shoud gibe me "Collision detection occurs and messages are sent upon collision".
Should I have also mesh collider to the moving building? It would complicate my code, because now the placed building is moving where is the mouse cursor. If I add collider to it at this stage it will block the ray to the terrain.
Like @$$anonymous$$iraSensei said: colliders can only collide with other colliders / triggers. Also the moving object should have a rigidbody attached. I'm still confused about your setup. Can you clearly explain what objects you have, what object should collide with what and do they have colliders attached.
Also note that a meshcollider can't collide with other meshcolliders unless it is a convex meshcollider. $$anonymous$$eshcolliders are quite heavy to compute so basically you would use them on static objects. $$anonymous$$oving objects are better off with primitive colliders (box, sphere, capsule)
$$anonymous$$y problem is solved. But its not related to the topic. "If collision" from a script was necessary.
I deleted if(hit.transform.tag == "Terrain") and I could use collider in the gameObject before placing it :-). I find OnTrigger... better becuse I can add reference to the game object by other.gameObject in void OnTriggerStay(Collider other). I still dont know how to find reference to my gameObject in OnCollisionEnter(Collision collision). Im not using OnCollision any more but it would be nice to know how find collided gameObject?
Thanks again
@menep: comments! Use comments if you don't answer the question.
OnCollisionEnter does actually give you a lot more information than just what object you collided with. Take a look at the Collision struct. collision.gameObject gives you the reference of the collided object. You get also all contact points. Each contact point gives you the exact point and collision normal vector which is very useful in many cases.
However collisions affect the objects movement since they are blocked by the physicssystem (penalty forces get applied when two objects intersect). Triggers are useful when you just want to detect that two objects touch / intersect. Triggers have no influence on the movement, they are "non solid".
Answer by KiraSensei · May 15, 2012 at 11:08 AM
If i'm not mistaking, to correctly use OnTriggerEnter you need colliders on both objects, a rigidbody on one of them only - very important - (the one moving for example..), and one of the isTrigger activated (the one attaching the script).
I actually had some issues with mesh collliders, so for the object with the rigidbody and the isTrigger (they both were on the same object for me), I applied a simple box collider. For a RTS game I think it will be OK (for both objects).
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