- Home /
Detecting collisions between blocks?
Is there any other was to detect collisions between two objects other than this:
function OnTriggerEnter(collision : Collider) {
if(collision.gameObject.tag=="Player"){
print("hit");
}
}
The problem with that is that it requires one of the objects to be a trigger meaning that physics do not apply to it. In the context of my application it means one object will pass through another which i don't really want.
Thanks in advance
Answer by AnaRhisT · Jun 20, 2010 at 02:12 PM
function OnCollisionEnter(collision : Collision) {
if(collision.gameObject.tag=="Player"){
print("hit");
}
}
Description OnCollisionEnter is called when this collider/rigidbody has begun touching another rigidbody/collider
I actually looked at this earlier but it didn't seem to work. Is there something about the objects that I have to change? It just never says "hit"
$$anonymous$$aybe you don't have colliders/rigidbodies or they're triggered..
I'm using a sphere and box collider. The following code is applied to them:
function OnCollisionEnter(collision : Collision) { print("hit1"); if(collision.gameObject.tag=="Player"){ print("hit2"); } }
Neither hit1 or hit 2 are printer. its rather odd.
Check if they're trigged. if they're not, try to use a rigidbody.
How odd, I use rigidbody on one of them with the default settings and it works. I change the settings so the block doesn't move and it stops working. Hmmmmm...
Answer by Nicolas 3D · Nov 30, 2012 at 04:53 PM
You can use OnTriggerenter, OnTriggerStay, OnTriggerExit, OnCollisionEnter, OnCollisionStay, OnCollisionExit :).
Answer by AlexHogan · Sep 06, 2013 at 07:36 PM
Yes, this is working as described in the docs - at least one of the colliding objects must have a non-kinematic rigidbody to make the collision events happen.
Collider.OnCollisionEnter(Collision) Description OnCollisionEnter is called when this collider/rigidbody has begun touching another rigidbody/collider.
In contrast to OnTriggerEnter, OnCollisionEnter is passed the Collision class and not a Collider. The Collision class contains information about contact points, impact velocity etc. If you don't use collisionInfo in the function, leave out the collisionInfo parameter as this avoids unneccessary calculations. Note that collision events are only sent if one of the colliders also has a non-kinematic rigidbody attached.
Your answer
Follow this Question
Related Questions
Using a trigger to align an object? 1 Answer
Collision returning an error 0 Answers
Box Collider Trigger Is True But Still Collide With Other Object 0 Answers
Detect a collision point, but allow pass through collider. 0 Answers
Detecting another object collision from another object script (2D) 0 Answers