- Home /
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.