- Home /
destroy object in front of an other object?
How can I delete the object in front of the camera?
var Voorwerp : GameObject; var positieVoor : Vector3;
if (Input.GetMouseButtonDown(1)){ Destroy(Voorwerp,positieVoor,Quaternion());
Comment
Best Answer
Answer by Statement · Mar 20, 2011 at 07:52 PM
Use Physics.Raycast.
This example script is meant to be placed on the camera. You might want to see the ref link about how to use layermasks to prevent destroying self.
function Update () {
if (Input.GetMouseButtonDown(1)) {
var hit : RaycastHit;
if (Physics.Raycast(transform.position, transform.forward, hit)) {
Destroy(hit.transform.gameObject);
}
}
}
Your answer
Follow this Question
Related Questions
Deleting one object, not all objects at once. 1 Answer
How to destroy a projectile when it leaves any side of the camera view on a 2D game? 1 Answer
Camera Orbiting Character: How to make an object move with another with no rotation 1 Answer
Destroy objects by clicking on them 1 Answer
Destroy an object with another one. 1 Answer