Sooo the game that I am currently making is a 3D top view using 2D sprites. And currently, I am successful in placing the buildings on the scene, the only problem is that you can place the buildings anywhere, except for when you try to place the buildings on top of each other. I want to be able to place the buildings on certain spots only, kinda like Sims.
This are my code for the building placement:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BuildingPlacement : MonoBehaviour {
private PlaceableBuilding placeableBuilding;
private Transform currentBuilding;
private bool hasPlaced;
public LayerMask buildingsMask;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Vector3 m = Input.mousePosition;
m = new Vector3 (m.x, m.y, transform.position.y);
Vector3 p = Camera.main.ScreenToWorldPoint (m);
if (currentBuilding != null && !hasPlaced) {
currentBuilding.position = new Vector3 (p.x, 0, p.z);
if (Input.GetMouseButtonDown (0)) {
if (IsLegalPosition ()) {
hasPlaced = true;
}
}
} /*else {
if (Input.GetMouseButtonDown (0)) {
RaycastHit hit = new RaycastHit ();
Ray ray = new Ray (new Vector3(p.x,8,p.z), Vector3.down);
if (Physics.Raycast (ray, out hit, Mathf.Infinity, buildingsMask)) {
Debug.Log (hit.collider.name);
}
}
}
*/
}
bool IsLegalPosition(){
if (placeableBuilding.colliders.Count > 0) {
return false;
}
return true;
}
public void SetItem(GameObject b){
hasPlaced = false;
currentBuilding = ((GameObject)Instantiate (b)).transform;
placeableBuilding = currentBuilding.GetComponent<PlaceableBuilding> ();
}
}
Quick Disclaimer: Your code is still hard to read in the way that it’s formatted on here. In the future please make an effort to make these things readable for us to try and help.
So I think I understand your question. You already have it so that buildings can’t be placed on top of one another correctly. But now you want it so that you can only place buildings in areas that you define are ok to build that building at.
I’m going to answer what I’ve stated above is true. I’m also going to be talking high level as the code may become tedious and you’ll want to come up with a system to handle all of this.
You can approach it two-ways; they’re not mutually exclusive. You can mark areas as safe to build on for certain types of objects, or you can mark areas as unsafe for certain types of buildings. I would make a scriptable object data type that can store if an area is safe or unsafe and for what kind of building. (ie. “Safe => Barracks”, “Unsafe => Power Plant”) Something like that. This gives you the ability to modify this information inside of the editor, create common groupings of these items in other scriptable objects, but also gives you the ability to extend this to include more information in the future if your game design changes. Maybe in the future, you’ll also want to account for how big of a certain kind of building you can build in that area or something, this will give you that ability.
Now that you have the scriptable object data, you’ll need to have a way to attach this to a new component that you make that will be attached to your terrain. This will define the area that you’re trying to build in. So if you’re on a grid, then you can define multiple grid locations with this new component. And this new component will take one, or many, of the scriptable objects stated above.
Now that you have these two things, you’ll be able to cast into your scene, get the component off of the area that the mouse is pointing to, and then compare the building you’re trying to build against the area component that you’ve selected and deemed whether or not it’s safe to build there.
With this method, you would probably need a default state as well so that you don’t have to specify every piece of your map, unless that’s what you want to do. It might make it easier to develop an editor tool so that you can paint these areas maybe, but that’s a whole separate thing. Let me know if you have questions about what I’ve described. Hope it helps.