Search Unity

how to find anothers objects position

Discussion in 'Scripting' started by morgansaysthis, Jun 10, 2007.

  1. morgansaysthis

    morgansaysthis

    Joined:
    Jan 12, 2007
    Posts:
    245
    im trying to make an empty GO stay half way between two moving objects, but i cant figure out how to tell the empty GO the positions of the other to objects



    how do i find where the other two objects are in world space to tell my empty GO??
     
  2. Willem

    Willem

    Joined:
    Mar 9, 2007
    Posts:
    184
    Every gameobject has a transform component so you should just be able to reference: (gameobject).transform.position to get the positions of each of your objects.
     
  3. morgansaysthis

    morgansaysthis

    Joined:
    Jan 12, 2007
    Posts:
    245
    i cant get it to work, is it supposed to look like this??




    Code (csharp):
    1. var box1dist=(box1).transform.position;


    it keeps telling me box1 is unknown
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You want to take those parentheses out.

    --Eric
     
  5. morgansaysthis

    morgansaysthis

    Joined:
    Jan 12, 2007
    Posts:
    245
    it still says unknown identifier, is this in the docs some where cause i couldn't find it, i could only find how to find the position of the object the script is attached to
     
  6. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Actually the parentheses aren't an issue; it's unusual but you can use parens like that.

    What you need to do is to declare box1 like this:

    Code (csharp):
    1.  
    2. var box1:Transform;
    3. var box2:Transform;
    4.  
    5. function Update() {
    6. //set position to the position of box1
    7. transform.position = box1.position;
    8. //set position to halfway between the two objects
    9. transform.position = Vector3.Lerp(box1.position, box2.position, 0.5);
    10. }
    11.  
    In the script's Inpector panel you'll see box1 and box2; drag your objects from the heirarchy onto these variables.

    This is in one of the tutorials somewhere, I think the FPS one.
     
  7. morgansaysthis

    morgansaysthis

    Joined:
    Jan 12, 2007
    Posts:
    245
    ahh thank you that works perfectly