- Home /
How do I get a Not collision?
I need to create a box with a collider that need to check if there is NO collision.
How can I can do this?
Edit:
It's needed to be checked as soon is possible, if I use a variable that is altered inside a function, then I have 1 frame delay.
If I need to create about 200 box it will be a delay about 3 seconds.
My goal is to create a pathfinder, it will create an box, that will create some boxes, that will create another boxes and so on.
So, I need the collision be checked as soon is possible, like in their creation.
Thanks.
Answer by Eric5h5 · Jan 28, 2011 at 08:53 PM
private var collisionCount = 0;
function OnCollisionEnter () { collisionCount++; }
function OnCollisionExit () { collisionCount--; }
function Update () { if (collisionCount == 0) { Debug.Log ("No collision"); } }
Question about OnCollisionExit(), when does it actually get fired? Any object that collides with a rigidbody isn't going to actually "exit" it, unless it is kinematic, right? Would "OnCollisionEnd" be a better name for it?
Answer by DaveA · Jan 28, 2011 at 06:00 PM
Check that there is a collision, and if none, assume not.
how? The OnCollisionEnter is a function. It is called just when there is a collision and don't have a return. I can't use a such thing like this: if(!Collider.OnCollisionStay(...))
If OnCollisionEnter is not called, assume it's not hit. $$anonymous$$inda like what Eric suggests
There is a big difference. Eric's version keep the information that there is actually no collision. OnCollisionEnter is only called in that frame where the intersection starts. During the collision OnCollisionEnter is not called anymore. OnCollisionStay is called every frame as long as there is a intersection.
I wouldn't say it's "better". It has its uses, but it's called every frame as long as there is at least one contact, so it's better if you don't use it if you don't need to. So in this case it would be worse.
Answer by Chilling · Mar 18, 2014 at 06:52 PM
This works for me (C#).
void FixedUpdate(){
staying = false;
}
void OnTriggerStay(Collider other){
if(other.collider.CompareTag("SomeTag")){
staying = true;
}
}
void Update(){
if(staying){
Debug.Log("Colliding");
}else{
Debug.Log("Not colliding");
}
}
Answer by Borgo · Jan 28, 2011 at 06:48 PM
I found this: Physics.CheckSphere
Returns true if there are any colliders touching the sphere defined by position and radius in world coordinates
Its check if a place is empty and return true or false.
The only problem is that it is not a cube, its a sphere.
Answer by PrimeDerektive · Jan 28, 2011 at 08:42 PM
private var wasHit : boolean = false;
function Update(){ if(wasHit == false){ Debug.Log("I am not being hit during this frame."); } }
function OnCollisionEnter (hit : Collision){ wasHit = true; }
That would have been my approach but don't use OnCollisionEnter it's better to use OnCollisionStay. And in your Update loop at the end you should set wasHit to false for the next frame.
Yeah that was a bit off the cuff just to get him started... I actually like Eric's solution the best.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Collider Error[SOLVED] 2 Answers
Cover mechanic - Restrict movement to box 1 Answer
Hit Collision with exceptions 2 Answers