- Home /
check If Building will collide with something before placed?
Hello. I have been wondering how I will be able to check if my item is going to collide with certain objects. If it does collide with certain objects I don't want it to place the object. The problem that I have right now is that I have a system in which it stops them from placing the object if my mouse is on top of the item. What I need is that it checks the area of my item and sees if it collides with something and if it does it wont place it. Here is my script that I have right now. PS: This script IS placed on the item that is instantiated in the game.
Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ItemMoveWithMouse : MonoBehaviour {
[SerializeField]
private LayerMask GroundLayer;
MakeABuilding MakeBuildingScript;
private void Start()
{
MakeBuildingScript = GameObject.Find("Main Camera").GetComponent<MakeABuilding>();
}
// Update is called once per frame
void Update () {
RaycastHit rayhit;
if(Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out rayhit, Mathf.Infinity, GroundLayer))
{
transform.position = rayhit.point + new Vector3(0, 1, 0);
}
if(Input.GetMouseButtonDown(0))
{
GameObject CurrentObject = rayhit.collider.gameObject;
if(CurrentObject.tag != "Villager")
{
MakeBuildingScript.isBuilding = false;
Instantiate(MakeBuildingScript.HousePrefab, transform.position - new Vector3(0, 1, 0), transform.rotation);
Destroy(gameObject);
}
else
{
Debug.Log("Someone Or Something is In The Way!");
}
}
if(Input.GetKeyDown(KeyCode.R))
{
Rotate();
}
}
private void Rotate()
{
transform.Rotate(new Vector3(0, 90,0));
}
}
Answer by Dragate · Oct 27, 2017 at 06:33 AM
OverlapBox() is made for this kind of thing. It will return to you an array of colliders that the box collider hits in the position and dimensions you define. It's like a raycast but with different geometry. If you don't care which colliders are hit in the area you want, you could use CheckBox() for simplicity. Instead of giving you an array of colliders it will just give you a bool (true if it hits anything, otherwise false). In either case, you use the function before instantiating your building and depending on their result you decide if it should instantiate or not.
https://docs.unity3d.com/ScriptReference/Physics.OverlapBox.html https://docs.unity3d.com/ScriptReference/Physics.CheckBox.html
Could you Possibly Show me An Example of this. I Do not understand the doc that well. Thanks!
void instantiateBuilding(Vector3 newPos, Vector3 extents){
//newPos is where you want to instantiate
//extents defines the the size of your box. If your building is like 20 units wide,
//10 high and 30 deep, then extents should be the half of those values (10, 5, 15)
Collider[] colliders = Physics.OverlapBox(newPos, extents);
for(int i = 0; i < colliders.Length; i++){
if(colliders[i].tag == "colliderToAvoid1" || colliders[i].tag == "colliderToAvoid2"){
//if a collider is found that you want to avoid then the function will return
//and thus, not reach the Instantiate()
return;
}
}
Instantiate(building, newPos, Quaternion.identity);
}
Hey I attempted to use your example but its not working. Here is the entire script. If you see any problems I would love it if you can help me. Sorry for the constant asking its just I am still pretty new to physics,collider and raycast related stuff. PS: $$anonymous$$y "newPos" is where my mouse is pointed.
using System.Collections;
using System.Collections.Generic;
using UnityEngine.AI;
using UnityEngine;
public class Item$$anonymous$$oveWith$$anonymous$$ouse : $$anonymous$$onoBehaviour {
[SerializeField]
private Layer$$anonymous$$ask GroundLayer;
$$anonymous$$akeABuilding $$anonymous$$akeBuildingScript;
public Vector3 extentbox;
private void Start()
{
$$anonymous$$akeBuildingScript = GameObject.Find("$$anonymous$$ain Camera").GetComponent<$$anonymous$$akeABuilding>();
}
// Update is called once per frame
void Update() {
RaycastHit rayhit;
if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out rayhit, $$anonymous$$athf.Infinity, GroundLayer))
{
transform.position = rayhit.point + new Vector3(0, 1, 0);
}
if (Input.Get$$anonymous$$ouseButtonDown(0))
{
GameObject CurrentObject = rayhit.collider.gameObject;
InstantiateObject(Input.mousePosition, extentbox);
}
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.R))
{
Rotate();
}
}
private void Rotate()
{
transform.Rotate(new Vector3(0, 90, 0));
}
void InstantiateObject(Vector3 newPos, Vector3 extent)
{
Collider[] collider = Physics.OverlapBox(newPos, extent);
for (int i = 0; i < collider.Length; i++)
{
if (collider[i].tag == "Villager" || collider[i].tag == "Villager")
{
return;
}
}
Instantiate(Instantiate($$anonymous$$akeBuildingScript.HousePrefab, transform.position - new Vector3(0, 1, 0), transform.rotation));
Destroy(gameObject);
}
}
Your answer
Follow this Question
Related Questions
How do you destroy a GameObject during the build process? 1 Answer
Question about Instantiation 1 Answer
How to make object visible without instantiating it? 2 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers