- Home /
Finding position of a game object
Hi,
I'm a complete beginner at Unity, and I've just started attempts at coding (I'm using it for my degree project, perhaps unwisely as I feel completely in over my head). My game aims to take the position of an object (which is controlled by a fiduciary marker) and being able to manipulate other game objects positions with it. However, I can't figure out even the basics so far, as I'm struggling to find the necessary javascript code to find the x and y co-ordinates of the object (it moves on a 2D plane, so Z isn't an issue). What is the code I need?
I have an idea how the code should look, but I'm just struggling to figure out how you'd write it in Unity. Heres a rough example of what I want it to do (obviously not in code yet).
Find the Xposition and Yposition of this gameObject (the one controlled by the fiducial, which I hope to apply this script to) For Zpos = 0 to 150, (this allows for depth) If a gameObject exists at (Xpos, Ypos, Zpos) Then that gameobject should gain an effect to show it has been found (glowing perhaps) If object is found, stop loop.
Sorry for being a completely beginner, I understand people normally come for help with a bit more than this.
Answer by Caiuse · Feb 20, 2011 at 06:59 PM
This will print the GameObject that it is attached to position in the Console. You could use that information for any other purpose
//Unity will constantly check the position of the GameObject.
function Update () {
//Creates a variable to check the objects position.
myPosition = transform.position;
//Prints the position to the Console.
Debug.Log(myPosition);
}
To get started:
http://forum.unity3d.com/threads/34015-Newbie-guide-to-Unity-Javascript-(long)
Thanks, I'll check those links out. I've never used Javascript before, only a little Visual Basic, so I was kinda lost. All of my searches were fruitless too.
I've only just started with javascript within the last month, I found UnityStudent a tutorial video website a brilliant start for learning. I was in noway a coder before, but know i know my way around(ish)
I'll check that out too thank you :D
Is there a way to pull out just one of the transform values (just x or y for example)? I imagine it'd be something like transform.position.something right? After that, no more annoying noobie questions for today, I promise.
So I got the code I needed working so far, and I tried it again with rotation, but the numbers come out as decimal points ins$$anonymous$$d of degrees. Thats fine, but why are there four of them? Obviously the first 3 are x,y and z rotation, so whats the fourth value?
Oh, nuts, I guess I lied about no more noobie questions for today >_<
Your answer

Follow this Question
Related Questions
Change Position of an object 1 Answer
Instantiate as a child at position 2 Answers
Position of a GameObject 2 Answers
2D Position of a game object. 1 Answer
How to tell if two blocks are right next to each other?(2D) 1 Answer