- Home /
How to destroy a gameObject when hit by Raycast?
After the explosion has been instantiated, I would like to destroy the gameObject I hit. Heres my code:
> if(Input.GetKeyUp(KeyCode.Mouse0)) {
> Debug.Log("Shot a bullet");
> var fwd = transform.TransformDirection
> (Vector3.forward);
> var hit :RaycastHit;
>
> if (Physics.Raycast (transform.position, fwd, hit, 60)) {
> Debug.Log ("Target Hit");
> Instantiate(bulletCheck, hit.point, transform.rotation);
> Instantiate(explosion, hit.point, transform.rotation);
> //Here I want to destroy the gameObject I hit.
> }
>
> }
Answer by aldonaletto · Jul 27, 2011 at 03:10 AM
The RaycastHit structure contains the hit object's transform, which can be used to access the gameObject property:
if (Physics.Raycast (transform.position, fwd, hit, 60)) {
...
Destroy(hit.transform.gameObject); // destroy the object hit
}
Thanks so much!
with help from another form i dont remember. I used this to destroy my object:
if (Input.GetButtonDown ("Fire1")) {
// Construct a ray from the current mouse coordinates var ray : Ray = Camera.main.ScreenPointToRay Input.mousePosition);
}
if (Physics.Raycast(ray, hit) && (hit.transform.gameObject == transform.gameObject)) {
Destroy(hit.transform.gameObject); Instantiate(smoke, transform.position, Quaternion.identity); }
Es increible que no haya un curso de unity y que nadie te explique bien como se usa cada cosa.
Answer by Chilling · Mar 09, 2015 at 01:03 AM
1 - Have a gameobject in the scene named "Camera" with a cameracomponent on it.
2 - Make sure there is a collider on the object you try to hit.
http://docs.unity3d.com/412/Documentation/ScriptReference/Physics.Raycast.html - at the bottom of the document.
(C#)
private Transform cameraTransform;
private Camera cam;
void Start(){
cameraTransform = GameObject.Find("Camera").transform;
cam = cameraTransform.camera;
}
void Update(){
if (Input.GetMouseButtonDown(0)) {
Ray ray = cam.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 10000)){
Debug.DrawLine(ray.origin, hit.point);
Debug.Log("Clicked on " + hit.transform.gameObject.name);
Destroy(hit.transform.gameObject);
}
}
}
Answer by LookDev · Dec 15, 2021 at 03:14 AM
if you want to make a game like an ant smash in 3d then this one is helping you
Camera mainCam;
// target layer set in inspector [ specific object with this layer ]
public LayerMask layer;
private void Awake () {
mainCam = Camera.main;
}
private void Update () {
// Calculate the distance
Ray ray = mainCam.ScreenPointToRay (Input.mousePosition);
Plane plane = new Plane (Vector3.up, Vector3.forward);
float rayDistance;
if(plane.Raycast(ray, out rayDistance)) {
Vector3 point = ray.GetPoint (rayDistance);
// Draw a line btw camera to end point
Debug.DrawLine (ray.origin, point, Color.red);
}
RaycastHit hit;
if(Physics.Raycast (ray, out hit, rayDistance, layer, QueryTriggerInteraction.Collide)) {
if(hit.collider != null) {
Destroy (hit.collider.gameObject);
Debug.Log (hit.collider.name);
}
}
Your answer
Follow this Question
Related Questions
Help destroying gameObject on RayCastHit? 1 Answer
Working Fine Then Get: No Cameras Rendering after destroying GameObjects 2 Answers
How do I detect if there's an object at the same position and destroy ONLY ONE of them? 1 Answer
My Raycast on awake fires and will not change Help! 0 Answers
Raycast hits and then doesn't fire again? 2D Game Raycasting Problem 1 Answer