I have now experienced my first steps in Unity and am having a lot of fun creating small games for my own use.
At the moment I would like to create a game that represents a theme park in order to work my way towards economic simulation and guests with individual needs.
I am therefore looking for a tutorial so that I can select a building from a list (building menu) at runtime and then place it. I would use a tilemap as a park base to simply check whether the required fields for the new building are free.
But I have no idea how to attach buildings to the mouse and then drag them to the target area and place them when the fields under the mouse are free.
Can anyone here recommend a tutorial? Gladly in German but English is also ok (I just have to concentrate a bit more :))
Thank you very much.
Greetings
Allbra
Hello @Allbra, If you don't manage to find any tutorial specifically for the building placement you can look for an inventory system which is pretty similar and then adapt it to your needs. Is your game in 2D or 3D? what do you mean by "attach buildings to the mouse" do you mean the buildings should follow the mouse or do you want them to be projected in 3D on the floor behind the mouse? Also as an experienced game developper I can make such system but it wouldn't be wise to just do it for you, you can cut the feature in parts first and try to make them one by one it'd be easier to help you
Hi @Cyber_Cats, I already have an inventory system. I can choose between three tabs in the inventory system and build buildings, do research or interact with the bank. At least the visuals are there. The logic will come bit by bit. I would now like to place one of the buildings from the inventory on the playing field. It would be nice if the planned building was semi-transparent on the mouse so that the player can see where it is going. It should be a 2D game. Thank you Greetings Allbra
If I understand well with your comment, you currently have an inventory with buildings on the side of the screen, you want to display the building on the mouse when we click on a building button, this newly “created” building will follow the mouse until placed on the terrain?
So how about for a first step we make your building “follow” the mouse when selected?
First you will change the building images in the inventory with buttons (so your inventory will be a list of buttons with the different building sprites as images), then when pressing the button it will activate an image that follows the mouse and changes it’s sprite for the building you selected (you can also set the color to transparent as you wish)
Let’s assume you are using a scroll view for your inventory, you will have a “Content” gameObject with all your building buttons as children.
Starting with this we will:
Add a script (you can name it “BuildingButton”) to every button in order to manage them easily
Create a gameObject in the scene (call it “BuildingPlacementManager” for example) and attach a script on it
the script BuildingButton will be like following:
using UnityEngine;
using UnityEngine.UI;
public class BuildingButton : MonoBehaviour {
public Button button; // don't forget to drag n drop the button
public Sprite sprite; // and fill the building sprite
}
the script BuildingPlacementManager will be like following:
using UnityEngine.UI;
using UnityEngine;
public class BuildingPlacementManager : MonoBehaviour {
public BuildingButton[] buildingButtons; // you will drag n drop every building button inside this array in your inspector
public MouseBuildingImage mouseBuildingImage; // this image will always follow the mouse and display the building that you are placing
private void Start() {
for (int i = 0; i < this.buildingButtons.Length; i++) {
int j = i; // since the variable "i" will be used in a callback we need it's current value (i can explain in detail later if you want)
this.buildingButtons[i].button.onClick.AddListener(() => this.SelectBuilding(this.buildingButtons[j].sprite));
} // this will add an event to every button with the building's data in order to display which building is selected
}
private void SelectBuilding(Sprite sprite) {
this.mouseBuildingImage.SetSprite(sprite);
this.mouseBuildingImage.Activate();
}
}
In your scene you will add a gameObject MouseBuildingImage that will have an image component on it but keep the image disabled, on this gameObject you can add a script to make it follow the mouse like this:
using UnityEngine;
using UnityEngine.UI;
public class MouseBuildingImage : MonoBehaviour {
public Image image;
private RectTransform _rectTransform;
private void Start() {
// Get the RectTransform component of the UI GameObject
this._rectTransform = this.GetComponent<RectTransform>();
}
public void SetSprite(Sprite sprite) {
this.image.sprite = sprite;
}
public void Activate() {
this.image.enabled = true;
}
private void Update() {
Vector3 mousePosition = Input.mousePosition; // Get the mouse position
// Update the position of the RectTransform to follow the mouse
this._rectTransform.position = mousePosition;
}
I’m not sure if it will work because I don’t have my computer to test right now but you can probably understand it well and fix it if you encounter any issue, or just send it here and we will be happy to help
Hi Cyber_Cats, many thanks for your reply. I will try it this weekend. Thank you.
Hello @Allbra, If you don't manage to find any tutorial specifically for the building placement you can look for an inventory system which is pretty similar and then adapt it to your needs. Is your game in 2D or 3D? what do you mean by "attach buildings to the mouse" do you mean the buildings should follow the mouse or do you want them to be projected in 3D on the floor behind the mouse? Also as an experienced game developper I can make such system but it wouldn't be wise to just do it for you, you can cut the feature in parts first and try to make them one by one it'd be easier to help you
– Cyber_CatsHi @Cyber_Cats, I already have an inventory system. I can choose between three tabs in the inventory system and build buildings, do research or interact with the bank. At least the visuals are there. The logic will come bit by bit. I would now like to place one of the buildings from the inventory on the playing field. It would be nice if the planned building was semi-transparent on the mouse so that the player can see where it is going. It should be a 2D game. Thank you Greetings Allbra
– Allbra