- Home /
Is gameObject present at coordinates (x,y,z)?
Hi, Bit of a beginner at unity and at Javascript coding in general. I'm making a 2d platformer game where objects can be controlled using a tracked fiducial marker. My main issue is that I struggle to find out what things are called in Unity in order to reference them in the code (the Unity resources are helpful, but sometimes a bit hard going).
Anyway, I've got some code which finds the x and y co-ordinates of a cube (and the rotation too) being controlled by the tracked fiducial, and now i want to use it's x & y co-ordinates in a loop to check if a gameobject exists at the co-ordinates ((myPosition), (myPosition2), z) on a loop where z = 0-50. After that i can work on making that secondary gameobject movable.
Heres my current code:
-function Update() { myPosition = transform.position.x; myPosition2 = transform.position.y; myRotation = transform.rotation.eulerAngles.z; Debug.Log(myPosition); Debug.Log(myPosition2); Debug.Log(myRotation); }
//function Update() { //For (z=0; z<50, z++){ //if (gameObject)
The code at the bottom is some early attempts at it before i realised i had no idea how to check if something was at a co-ordinate. I'm assuming my idea that i could just check if gameobject = true at co-ordinates (myPosition, myPosition2, z) is probably wrong, as I think that would end up as a vector3 instead of a location co-ordinate?
So my issues are 1. How do I check if a gameobject exists at a location? 2. can i use variables in a co-ordinate for a location to check? 3.Is a For loop the best way to check the z distance at my x,y co-ordinates, or am i doing this all wrong? 4. Oh and lastly, as a bonus question, once i find an object, i need to stop the loop. how do i do that?
Sorry for lots of questions, hopefully i'll get the hang of this quickly and bother you guys less :D
Never compare exact positions. Positions are floats so a x-value of 50.3 is NOT equal to 50.30001 but that difference can't be seen. I think you want to use a BoxCollider ins$$anonymous$$d of a sphere and use what Simple said. Triggers are very common in all kinds of games. if you enter the area inside the box or sphere of your trigger OnTriggerEnter is called. There are a lot similar questions around here.
Answer by Simple · Feb 21, 2011 at 01:33 PM
In my opinion you should use OnTriggerEnter(col:Collider) and OnTriggerStay(col:Collider). http://unity3d.com/support/documentation/ScriptReference/Collider.OnTriggerEnter.html
1-3. So: Attach trigger sphere with radius 50(or what u need) to your marker position. Just create sphere, make it a trigger, set its coords equal to your marker coords. And it should be a child of your marker. Then write a script using OnTriggerEnter(col:Collider) and OnTriggerStay(col:Collider). Attach script to sphere. And when smth enters this trigger it will call function OnTriggerEnter which should send collision(better) or 'true'(not very good) to your main script.
4 To stop loop use next constructions:
for (k=0; k<50; k++)
{
//some code
//ex1t=true when obj find
//...
if (ex1t) break; //break exits loop; ex1t - any boolean var
}
OR
for (k=0; (k<50)&&(!ex1t); k++) //loop continues while ex1t is false and k<50
{
//ex1t=true when obj find
//...
}
If I'm understanding this right, the sphere will be at a fixed distance from my marker right? The problem with that method is that i want to be able to find objects at a variety of different depths (hence the loop). If multiple objects appear at the markers x,y co-ordinates I want to find only the closest one (hence wanting to leave the loop if one is found).
Is there a way of making this collider code work like that?
As I said trigger sphere center has same position as your marker. So sphere will be around your marker and cover all depths what u need(e.g. by changing trigger radius from 0 to 50)
Ah O$$anonymous$$, I guess i misunderstood, thanks. i'll report back soon on how it worked out.
Ok, this is weird. I'm using a box collider (because i needed z axis depth, but not X or y, and it's working at finding collision with only one of my gameobjects - the one WITHOUT a collider or a rigid body. all my others, which have rigidbodies and colliders, aren't activating the collision at all. I thought i was only meant to work with other colliders? I'm a bit confused.
Your answer
Follow this Question
Related Questions
Finding position of a game object 1 Answer
Different child position when parent gameobject selected 0 Answers
Instantiating Multiple Game Objects to the Position of a Current GameObject 2 Answers
How to Position a GUI at the Top-Right of the Screen? 2 Answers
Using external GPS device unity application -1 Answers