- Home /
how do i make my objects not spawn on already spawned objects
Im making a sim city game and I need it to where the buildings will not spawn in any game object in the game. Please help. Script is in c#
using UnityEngine; using System.Collections;
public class MouseClick : MonoBehaviour {
public GameObject building;
public Collider coll;
void Update() {
if (Input.GetMouseButtonDown(0)) {
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitInfo = new RaycastHit();
if(coll.Raycast(ray, out hitInfo, Mathf.Infinity)){
newBuilding ( hitInfo.point );
}
}
}
void newBuilding(Vector3 Building){
GameObject building1 = (GameObject)Instantiate(building);
building1.transform.position = Building + new Vector3 (0, 0.01f, 0);
}
}
Comment
Answer by Inok · Oct 06, 2015 at 06:57 PM
You need to check that point with some radius from it where you place new building is clear. For this make Physics.SphereCast with radius of building and check that this SphereCast not hit building collider (you can check by different ways: compare tag, layer, name or detect if hitted gameobject have some script...). Not sure that this is perfect solution but it can work if do it smart.