- Home /
Question by
Crazy_Minecrafter · Jan 11, 2020 at 01:46 PM ·
unity 5raycast3ddestroy object
Destroy the object i am looking UNITY 3D
Hello, I am making a building game and I want to click and destroy some blocks I have placed before. I made a script in the blocks(they are clones)
if (Input.mousePosition == gameObject.transform.position)
{
if (Input.GetMouseButtonDown(1))
{
Destroy(gameObject);
}
}
Any solution how can I make this work? Maybe with raycast but I tried something and didn't work. Thanks
Comment
Answer by BestPoly · Jan 15, 2020 at 07:57 AM
function Update ()
{
var hit : RaycastHit;
var ray = Camera.main.ScreenPointToRay ( Input.mousePosition );
hit = Physics.Raycast (ray, 30);
if (hit && Input.GetMouseButtonDown(0))
{
Destroy(hit.collider.gameObject.name);
}
}