- Home /
Null Collision Detection
Hello!Sorry for the newbish question but how can i create a boolean variable that returns true if the object is colliding and false when it is not colliding with anything?I searched for it all the morning but found nothing.
var bool : boolean = true;
function OnCollisionStay(){ bool = true; }
function OnCollisionExit(){ bool = false; }
^ That didn't worked because as my object exits the collision on another object it will return true all of the time. :[ Please help me
Answer by Aldwoni_legacy · May 10, 2011 at 09:53 AM
Have you tried OnCollisionEnter instead of OnCollisionStay?
And beside that i don't want to test when it exits a collision,i want it to tell me when it is not colliding with something.Like : When my cube is in the air bool = false and when it is on the ground or touching something bool = true
Answer by Senhor de todo o Mal · May 10, 2011 at 10:11 AM
Try this:
var colliding:int = 0;
function OnCollisionEnter(collision : Collision):void{ colliding++; }
function OnCollisionExit(collision : Collision):void{ colliding--; }
Note that collision events are only sent if one of the colliders also has a non-kinematic rigid body attached.
EDIT:
You can also try using Physics.OverlapSphere or Physics.CheckCapsule instead.
Hmm.Looks like both of them worked if i would have used rigidbody.addForce or addVelocity ins$$anonymous$$d of rigidbody.position = Vector3(0,1,0) for leaving the collison.But why?I need to use the position one since i need my object has to follow the curosr
If i use something like : if(Input.Get$$anonymous$$ey("b")){rigidbody.position = anotherGameObject.position} OnExitCollision wont work
When you alter position directly, collision is not checked.
Hmmm,then is there any way of checking if my object is touching another object with a collider on it?
You can try using Physics.CheckCapsule or Physics.OverlapSphere
Your answer
