- Home /
I need help for code to distinguish the difference between two game objects with the same tag
My code right now can move one of the white blocks and allow it to stay between the walls, not get pushed out. Similar to the box/text movement in baba is you. What I want to do is have multiple boxes that follow this same movement. The boxes and player are being tracked by their Vector3 on the game by their tag. Is there any way that I can use the same tag, but it can allow for the boxes to move independently? The error right now is that it is only tracking one of the boxes and then when I move that box, the other block teleports to the location. Using transform.position for the movement and since the player was shifting to the side slightly the positions and rotations are frozen.
public class BlockPushing : MonoBehaviour { private Vector3 player; private Vector3 block; private Vector3 Top; private Vector3 Left; private Vector3 Right; private Vector3 Down; private Vector3 Wall; public float speed = 1.25f; private bool OnOff = true; // Update is called once per frame void Update() { player = GameObject.FindGameObjectWithTag("player").transform.position; block = GameObject.FindGameObjectWithTag("block").transform.position; Top = GameObject.FindGameObjectWithTag("Up").transform.position; Left = GameObject.FindGameObjectWithTag("Left").transform.position; Right = GameObject.FindGameObjectWithTag("Right").transform.position; Down = GameObject.FindGameObjectWithTag("Down").transform.position; Wall = GameObject.FindGameObjectWithTag("Wall").transform.position;
//make the block move when pushed
//the player.x == block.
if (Input.GetKeyDown(KeyCode.G) && (player.x != block.x || player.y != block.y))
{
if (OnOff)
{
OnOff = false;
}
else
{
OnOff = true;
}
}
if (player.x == block.x && block.y == player.y && OnOff)
{
//left
if (Left.x + speed < block.x)
{
if (Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.A))
{
transform.position = new Vector3(block.x - speed, block.y, 1);
}
}
//right
if (Right.x - speed > block.x)
{
if (Input.GetKeyDown(KeyCode.RightArrow) || Input.GetKeyDown(KeyCode.D))
{
transform.position = new Vector3(block.x + speed, block.y, 1);
}
}
//up
if (Top.y - 2* speed > block.y)
{
if (Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.W))
{
transform.position = new Vector3(block.x, block.y + speed, 1);
}
}
//down
if (Down.y + 2 * speed < block.y)
{
if (Input.GetKeyDown(KeyCode.DownArrow) || Input.GetKeyDown(KeyCode.S))
{
transform.position = new Vector3(block.x, block.y - speed, 1);
}
}
}
} }
public class BlockPushing : MonoBehaviour { private Vector3 player; private Vector3 block; private Vector3 Top; private Vector3 Left; private Vector3 Right; private Vector3 Down; private Vector3 Wall;
public float speed = 1.25f;
private bool OnOff = true;
// Update is called once per frame
void Update()
{
player = GameObject.FindGameObjectWithTag("player").transform.position;
block = GameObject.FindGameObjectWithTag("block").transform.position;
Top = GameObject.FindGameObjectWithTag("Up").transform.position;
Left = GameObject.FindGameObjectWithTag("Left").transform.position;
Right = GameObject.FindGameObjectWithTag("Right").transform.position;
Down = GameObject.FindGameObjectWithTag("Down").transform.position;
Wall = GameObject.FindGameObjectWithTag("Wall").transform.position;
//make the block move when pushed
//the player.x == block.
if (Input.GetKeyDown(KeyCode.G) && (player.x != block.x || player.y != block.y))
{
if (OnOff)
{
OnOff = false;
}
else
{
OnOff = true;
}
}
if (player.x == block.x && block.y == player.y && OnOff)
{
//left
if (Left.x + speed < block.x)
{
if (Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.A))
{
transform.position = new Vector3(block.x - speed, block.y, 1);
}
}
//right
if (Right.x - speed > block.x)
{
if (Input.GetKeyDown(KeyCode.RightArrow) || Input.GetKeyDown(KeyCode.D))
{
transform.position = new Vector3(block.x + speed, block.y, 1);
}
}
//up
if (Top.y - 2* speed > block.y)
{
if (Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.W))
{
transform.position = new Vector3(block.x, block.y + speed, 1);
}
}
//down
if (Down.y + 2 * speed < block.y)
{
if (Input.GetKeyDown(KeyCode.DownArrow) || Input.GetKeyDown(KeyCode.S))
{
transform.position = new Vector3(block.x, block.y - speed, 1);
}
}
}
} }
Answer by rh_galaxy · May 03 at 08:08 PM
Without having read the code at depth, maybe you can set the name of the object differently when you create/instantiate them as well as the tag. Also GameObject.FindGameObjectWithTag() is not something you should run every frame (in Update()) because it is costly, do it once in Start() and keep the info so you can use it in Update().
Answer by Boucer_Boy · May 12 at 07:40 PM
I've solved my problem so I'll share it here in case it might help someone.
public float[] numberblocks; public int NumberOfBlocks; //change this in the unity UI
Awake(){ numberblocks = new float[NumberOfBlocks]; }
for (int x = 1; x <= NumberOfBlocks; x++) {
numberblocks[x-1] = "" + x;
block = GameObject.FindGameObjectWithTag("block" + x).transform.position;
// just make sure the tags increase numarically
}
Your answer