Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by JustinVos · Jul 27, 2011 at 01:49 AM · raycastdestroyraycastinghit.point

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.
 >             }
 >             
 >         }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
10
Best Answer

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
 }

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image nice1013 · Jan 12, 2013 at 10:42 PM 0
Share

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); }

avatar image dnfabbiano · Aug 13, 2014 at 01:07 AM 0
Share

Es increible que no haya un curso de unity y que nadie te explique bien como se usa cada cosa.

avatar image
5

Answer by Peter G · Jul 27, 2011 at 03:06 AM

Try:

 Destroy(hit.collider.gameObject);
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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);
         }        
     }
 }















Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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);
         }   
     }



Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges