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);
}
}
}
}
}