- Home /
hit something with collider
how can I do it that if something hits something it does something?
for example:
if(Robot.hit(this)){ blabla(); }
Answer by LegionIsTaken · Jun 19, 2011 at 12:34 PM
function OnCollisionEnter(hit : Collision)
{
if(hit.gameObject)
{
blabla();
}
}
Answer by Merglasch · Jun 19, 2011 at 12:42 PM
Hey there,
Take a look into the Unity Script Reference. There are 6 functions for collision detection.
OnTriggerEnter
OnTriggerStay
OnTriggerExit
OnCollisionEnter
OnCollisionStay
OnCollisionExit
They're used like this:
void OnTriggerEnter(Collider other){ //the collider other collides with assigned object
do this
}
Be aware you need to attach colliders,triggers and rigidbodys for your collisions to occur
Here's a link to the reference: http://unity3d.com/support/documentation/ScriptReference/30_search.html?q=collision
also you can check the manual about Rigidbodies. There's some useful information in there
regards
and how can I do it that it does something when it hits anything?
since you can define the collider other yourself that's pretty much everything isn't it? leaving the collider empty might also work (depends)
Also at the OnCollision events the Collision information is proceeded ins$$anonymous$$d of the collider info. (void OnCollisionEnter(collisionInfo : Collision).
well I want that it does a sound when it hits something could anybody do a code for this because i tried diefferent things but nothing worked
Answer by bubblegumsoldier · Jun 19, 2011 at 02:23 PM
and how can I do it that it does something when it hits anything?
(I wrote a comment but nobody answered so sorry for this "answer".)
Your answer
Follow this Question
Related Questions
Raycast Destroy(hit.collider.gameObject); (Still need help) 1 Answer
Accesing Player colliderhit with other gameobject? 2 Answers
The Child Is Not Hovered When Passing The Parent's Collider 0 Answers
Raycast2D doesn't detect objects 1 Answer
ERROR: why is showing red OnControllerColliderHit? 2 Answers