x


Object position question...

I am trying to set up a system in my game that allows the position of two objects that are surrounding the object that has the script attached to it that are within a certain range (In this case, I want one object on the left and one on the right, so I would check the positions along the x axis). Then, when a certain key is pressed, I want to switch the positions of the two objects (so the one that was on the left will now be on the right and vice versa). If it helps here is a video showing what I want (the cursor that switches the blocks in this game, just imagine there is a game object in the center of the cursor, between two blocks).

My idea was to: 1 check the scene for the type of object I want to switch(I would do this every second or so, as to not slow down the game that much), in this case, that means objects with the tag "block". 2 Get the positions of two objects specific that I want. 3 Then, when the player clicks down, invoke the function that switches the objects.

I am a little confused on exactly what to do. How can I get the positions of the objects and how can I isolate the two that are within the range from object (the way my game is laid out, there will only be two within that range at all times, its a puzzle game and the objects laid out in a grid of tiles)?

Here is code I have so far. (A and B are the two objects on the left and the right

private var A : GameObject;
private var B : GameObject;

function Start  () {
    InvokeRepeating("Find",0,1);
    }
function Find   () {
    /*SOMETHING*/
}
function Update () {
    if( Input.GetButtonDown( "Fire1" )){{if(GameObject.position - B).x <= 1 && (GameObject.position - A).x >=-1 && <=0){Invoke("Switch",0)
    }
   }
  }
}

function Switch () {
    B.transform.position = A.transform.position; A.transform.position = B.transform.position;
    }

I know that was alot, but thank you.

more ▼

asked Feb 05 '11 at 05:15 AM

Tyler 2 gravatar image

Tyler 2
1.1k 211 246 264

(comments are locked)
10|3000 characters needed characters left

0 answers: sort voted first
Be the first one to answer this question
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1095
x886
x798

asked: Feb 05 '11 at 05:15 AM

Seen: 730 times

Last Updated: Feb 05 '11 at 05:15 AM