- Home /
Question by
lukssoftware · Aug 27, 2015 at 12:20 PM ·
prefabspawningarea
Spawn prefab only in allowed area?
Hello, I'm using this code to spawn a prefab at mouse position, but I only want it to be allowed to be spawned in the area next to street. How can I do this?
if (place_power_plant_coal && Input.GetButtonDown("Fire1")) {
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit)) {
Instantiate(__power_plant_coal, hit.point, Quaternion.identity);
}
}
Comment
Answer by graciasluigi · Aug 27, 2015 at 12:43 PM
You could use layers to define the "spawnable" areas, using the RayCast method but with a layermask.
http://docs.unity3d.com/ScriptReference/Physics.Raycast.html http://answers.unity3d.com/questions/8715/how-do-i-use-layermasks.html
The solution suggested by @Muuskii is a possibility too: http://answers.unity3d.com/questions/119908/instantiating-inside-an-area-only.html