Using Unity 2D
I have a game that uses hot keys, pressing “X” brings up the roads template, and “Z” brings up the building template. The problem is, which you will see in the diagrams below, after pressing the “Z” key once, then pressing the “X” key the road template is on top of the building template picture. You can move the mouse to where you want to place the template, then left click to place it.
My “template” or “blueprint” if you will, is by lowering the alpha color channel and then when you place it in the game world by clicking the mouse button, the image turns into a solid color (raising the alpha color channel)
They do have a sprite renderer, layer is set to default so not a UI element.
Diagram A: After pressing “Z”:
Diagram B: After pressing “Z” first then while the selection is still open, you press “X” (notice how the road (blue) is on top of the building template image.
How do I fix it so you dont have to clear the current selection (do that by pressing the ESC key) so they dont overlap? Diagram C:
I dont do a very good job explaining, so I hope the diagrams helped. Below is the script for the building hot key which is “Z”
//CODING THE MOUSE POSITION, THE Z KEY, AND MOUSE TO MOVE THE SELECTED TEMPLATE AROUND AND THE Z TO OPEN THE SELECTION
private void Update()
{
Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector2 spawnPos = new Vector2(Mathf.Round(mousePos.x), Mathf.Round(mousePos.y));
if (Input.GetKeyDown("z") && isAnObjectSelected == false) //doesnt have an object selected
{
currentlySelectedObject = (GameObject)Instantiate(selectableObjects[selectedObjectInArray], spawnPos, Quaternion.identity); //create but also storing
isAnObjectSelected = true; //to now say that we got one out
}
}
{
//----------------------------------------------------------------------------------------------
//CODING THE ESCAPE KEY TO CLEAR THE SELECTED OBJECT
if (Input.GetKeyDown(KeyCode.Escape) && isAnObjectSelected == true) //if object is selected, and we press the "escape" key, it will clear it
{
Destroy(currentlySelectedObject); //deletes the selected template
isAnObjectSelected = false;
selectedObjectInArray = 0; //resets it back to just the mouse cursor with no template
}
//-------------------------------------------------------------------------------------------------
//CODING THE SCROLL WHEEL TO SCROLL THROUGH THE DIFFERENT OBJECTS IN THE SELECTION
if (Input.GetAxis("Mouse ScrollWheel") > 0 && isAnObjectSelected == true)
{
selectedObjectInArray++;
if (selectedObjectInArray > selectableObjects.Length - 1)
{
selectedObjectInArray = 0;
}
Destroy(currentlySelectedObject);
currentlySelectedObject = (GameObject)Instantiate(selectableObjects[selectedObjectInArray], spawnPos, Quaternion.identity);
}
else if (Input.GetAxis("Mouse ScrollWheel") < 0 && isAnObjectSelected == true)
{
selectedObjectInArray--;
if (selectedObjectInArray > selectableObjects.Length - 1)
{
selectedObjectInArray = 0;
}
Destroy(currentlySelectedObject);
currentlySelectedObject = (GameObject)Instantiate(selectableObjects[selectedObjectInArray], spawnPos, Quaternion.identity); //if brackets are correct and still get a red underline under one of them, check the end of the statement to see if it has a semi-colon
}
}