Thursday, May 10, 2018

unity - How to detect if instantiated prefab can be placed at current location or not


I'm using Unity 5.5.0f3 with C#.


My prefab(wooden fence) is instantiated at my mouse location when I click a UI button and follows my mouse until I click somewhere in the scene to place it. The fence has a box collider on it.


Currently, the fence is able to be placed intersecting and even inside of anything else on the scene right now, and I want to eliminate this issue.


I would like my fence (and any other future prefabs) to check if it is intersecting an object's collider when it is following my mouse and turn red if it is, or else be green if it is not to indicate if it can or cannot be placed there and either allow or deny placement accordingly.


I have read that there is no way to do this using transform.position since this is essentially using teleportation on my fence. I tried to use GetComponent().MovePosition(hit.point + positionOffset); but I get all kinds of weird behavior when I do this.



The following are the three scripts I am using to make this happen:


Raycast Builder:


public class RaycastBuilder : MonoBehaviour {

public GameObject defense;
public Vector3 positionOffset;

public bool IsPurchased = false;

public float speed = 2f;


Vector3 mousePosition;

BuildManager buildManager;

void Start ()
{
buildManager = BuildManager.instance;
}


void Update ()
{
GameObject defenseToBuild = buildManager.GetDefenseToBuild();
if(defenseToBuild == null)
{
return;
}

//To get the current mouse position
mousePosition = Input.mousePosition;


Ray ray = Camera.main.ScreenPointToRay(mousePosition);
RaycastHit hit;

if (Physics.Raycast(ray, out hit, 1 << 8))
{
defenseToBuild.transform.position = hit.point + positionOffset;

if (Input.GetKey(KeyCode.E))
{

defenseToBuild.transform.Rotate(Vector3.up * speed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.Q))
{
defenseToBuild.transform.Rotate(-Vector3.up * speed * Time.deltaTime);
}
}

if (Input.GetMouseButton(0)&& IsPurchased == true)
{

buildManager.SetDefenseToBuild(null);
IsPurchased = false;
}

if(Input.GetKeyDown(KeyCode.Escape))
{
buildManager.SetDefenseToBuild(null);
Destroy(defenseToBuild);
IsPurchased = false;
}

}
}

Shop:


public class Shop : MonoBehaviour {

BuildManager buildManager;

void Start ()
{

buildManager = BuildManager.instance;
}

public void PurchaseWoodenFence ()
{
Camera.main.GetComponent().IsPurchased = true;
Debug.Log("Wooden Fence Started");
buildManager.SetDefenseToBuild((GameObject)Instantiate(buildManager.woodenFencePrefab, Vector3.up, Quaternion.identity));
}


}

Build Manager:


public class BuildManager : MonoBehaviour {

public static BuildManager instance;

void Awake ()
{
if (instance != null)

{
Debug.Log("More than one BuildManager in Scene!");
return;
}
instance = this;
}

public GameObject woodenFencePrefab;

private GameObject defenseToBuild;


public GameObject GetDefenseToBuild ()
{
return defenseToBuild;
}

public void SetDefenseToBuild (GameObject defense)
{
defenseToBuild = defense;
}


}


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...