- Home /
making position checker - is area empty or not (with trigger)?
I'm doing a ghost object, that checks is there any place for new object. I'm having a function:
function Ghost(parent : GameObject)
{
var checker : GameObject = GameObject.CreatePrimitive(PrimitiveType.Cube);
checker.AddComponent ("Rigidbody");
checker.collider.isTrigger = true;
checker.transform.parent = parent.transform;
checker.transform.localPosition = Vector3(0,0,Distance);
need something like on collider enter inside function || that will give a true or false value on return
Destroy (checker); }
I tried to add some script, with returning value on trigger enter... But unity returned me beginning value - its like unity don't have a time to check trigger - he just immediately returns a beginning value..
Is there any way to do it from a function like I have, without a new scripts.. All I need to add to checker gameObject, something like - if (checker.trigged) return false.
You are going to need to get a list of the things it might hit and check whether their bounding boxes overlap with your new object. I'll try to post an answer when I'm somewhere near a computer rather than on a iPad :)
Or you could do a ray cast from the centre of the "to be placed" object to each of the corners of it's area, which would work in many cases but would miss objects just below the centre point.
Answer by aldonaletto · Jun 30, 2012 at 05:27 PM
You're almost there: just wait for the next FixedUpdate before destroying your ghost object - collisions are detected in the physics cycle. But you should disable its mesh renderer, or the ghost would appear for a while - very creepy! Another point: the Ghost function must be called in a coroutine, because you must wait its end to know the result.
Put all things together, your script could become something like this:
var somethingIsThere = false;
function WarnDaddy(){ // ghost calls this function if something detected somethingIsThere = true; }
function Ghost(parent : GameObject){ var checker : GameObject = GameObject.CreatePrimitive(PrimitiveType.Cube); checker.AddComponent(GhostScript); // you need a script in your ghost! checker.AddComponent(Rigidbody); checker.renderer.enabled = false; // you don't want the ghost appearing... checker.rigidbody.isKinematic = true; // avoid gravity or other physics effects checker.collider.isTrigger = true; // define rotation and position based on "parent" object... checker.transform.rotation = parent.transform.rotation; checker.transform.position = parent.transform.TransformPoint(Vector3(0,0,Distance)); // but child the ghost to THIS object to get the result: checker.transform.parent = transform; somethingIsThere = false; // clear flag yield WaitForFixedUpdate(); // wait collision detection Destroy(checker); // exorcise ghost }
// How to use: call Ghost via yield, then read the somethingIsThere variable:
yield Ghost(someObject); if (!somethingIsThere){ // there's nothing in that place } Ghost script (GhostScript.js):
function OnTriggerEnter(other: Collider){ // if something is there, warn your daddy: transform.parent.SendMessage("WarnDaddy"); // call parent function WarnDaddy }
thanks man! Its working perfectly! You can not imagine how much I'm happy! Thank you! Its exactly what I wanted!
Answer by irrationalistic · Jun 29, 2012 at 06:13 PM
If you are ok with checking a sphere, look at the OverlapSphere function. This will return a list of all objects that intersect that given sphere. Then you just see if that is zero and place your object.
I need to check area with box, that can change sizes, sphere isn't that what I needed.
You could use the overlap sphere (with a radius equal to the furthest possible point on your target bounding box) to get all potential objects in the area without having to ray cast all objects in your scene. Once you have your potential list, use your given box collider and check out the intersects values compared to each potential object. This will cut back on the number of potential physics tests while still allowing you to check the box.
Your answer
Follow this Question
Related Questions
What's the best way to check if an area is empty? 7 Answers
Help with a script 2 Answers
linking particlesystem to cube,conecting particlesystem to cube 0 Answers
kinematic trigger with static collider-no trigger message? 0 Answers
OnTriggerEnter() problem 1 Answer