- Home /
How to move Instantiated 2D objects by 0.5 using arrows(or mouse)
I have this block breaker type of game. Where you start the ball and then it goes bouncing around, and you have to hit it back with a paddle. Now I want to have God Mode, where user can design a level him/herself. Each block is 1x1 size and there are 4 types of blocks as you can see. I have managed to code instantiating each with different keys(1,2,3,4), but now I have trouble designing them around. They all instantiate in the middle of the screen (0,0), on top of each other, and if the user might instantiate like 200 of them, I worry, the game might lag or something. Because the screen is 16x12. Here's my code for instantiating these prefabs, what can I do to improve and move them around, save and add next prefab and finally finish it. If they could test, it would be even better.
public GameObject block3hit;
public GameObject block2hit;
public GameObject block1hit;
public GameObject unbreakableBlock;
void Start()
{
}
private void Update()
{
SpawnBlock3();
SpawnBlock2();
SpawnBlock1();
SpawnBlock4();
}
void SpawnBlock3()
{
if (Input.GetKeyDown(KeyCode.Alpha3))
{
GameObject k = Instantiate(block3hit);
k.transform.position = new Vector2(0, 0);
}
}
void SpawnBlock2()
{
if (Input.GetKeyDown(KeyCode.Alpha2))
{
GameObject k = Instantiate(block2hit);
k.transform.position = new Vector2(0, 0);
}
}
void SpawnBlock1()
{
if (Input.GetKeyDown(KeyCode.Alpha1))
{
GameObject k = Instantiate(block1hit);
k.transform.position = new Vector2(0, 0);
}
}
void SpawnBlock4()
{
if (Input.GetKeyDown(KeyCode.Alpha4))
{
GameObject k = Instantiate(unbreakableBlock);
k.transform.position = new Vector2(0, 0);
}
}
Answer by Padalino · Apr 14, 2020 at 09:05 PM
Hello! This might sound a bit tricky, but I have an idea:
Create a boolean variable like blockCreated, that indicates that the player has created a block. Set it to true if the player creates a block. The idea of this is preventing the player from creating another block in the meantime, so you should change your if's to something like:
if (Input.GetKeyDown(KeyCode.Alpha2) && blockCreated == false)
{
GameObject k = Instantiate(block2hit);
k.transform.position = new Vector2(0, 0);
blockCreated = true;
}
Then, create a script and add it to the block's prefab. In that script, you should add a Movement code, to place the block wherever the player wants. You will also need to retrieve your BlockGeneratorScript, either by creating a GameObject variable and dragging the GameObject that contains the script from the hierarchy, or by coding a reference to it (like using the gameObject.FindWithTag option).
So, at this point, you have one block created and the player can't create more, but the player is able to move the newest block around. Once the block is placed in the desired position, it should keep there. Just create a function in your blockMovement script that does the following:
Set the blockCreated variable to false. To access another scripts variable, just do something like this:
ScriptName variableForTheScript = GameObjectWithTheBlockGeneratorScript.getComponent<ScriptName>()
Then, just reference it like:variableForTheScript.blockCreated = false
Destroy (disable) the script in the created block by typing gameObject.ScriptName.enabled = false. This way, the player won't be able to move it more.
Note that this is a pseudocode, and may sound more difficult than it really seems. But my approach would be: create a block, prevent the player from creating another block, move the block around, press a button to set the final position of the block, let the player create another block.
Hope that helps (getting an idea, at least)!