Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
  • Help Room /
avatar image
0
Question by Devastator TF2 · Jan 05, 2013 at 05:13 PM · raycastdestroytagsray

(Solved!) How to Destroy tagged items using raycast?

Thanks to everyone who helped im very grateful. The Script Works and destroys the object fine.

This is my Code EDIT : Changed it but still does not work

UPDATED SCRIPT Thanks To alucardj

PS

! I can hit the object and it shows in the debug but it still doesn't destroy it !

   if (Input.GetMouseButtonDown(0)){       
     var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
     var hit : RaycastHit;
 
     if (Physics.Raycast(ray, hit, 1000)){ // 1000 or Mathf.Infinity should be enough !
         // what did the raycast hit ?
         Debug.Log( "ray hit (name): " + hit.collider.gameObject.name);
         Debug.Log( "ray hit (tag): " + hit.collider.gameObject.tag );
 
         if(hit.collider.gameObject.tag == "prop")
         {           
             Destroy(hit.collider.gameObject);
         }
     }
 }
Comment
Add comment · Show 1
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 VaporVx · Oct 10, 2018 at 01:06 PM 1
Share

This was exactly what I needed, thankyou for putting up your code. $$anonymous$$ine was pretty close to this at the start, but i was using 2D objects. Somehow after paying attention to the missing collider call, I decided to to change my 2D objects to have normal box colliders and it worked.` public Transform Effect; public float damage = 100f;

 // Update is called once per frame
 void Update () {
     Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
     RaycastHit hit;

     if (Input.Get$$anonymous$$ouseButtonDown(0))
     {
         if(Physics.Raycast(ray, out hit, 1000))
         {
             hit.transform.Send$$anonymous$$essage("ApplyDamage", damage, Send$$anonymous$$essageOptions.DontRequireReceiver); //calls enemyhealth script and provides parameter
             /*if(hit.transform.tag == "Fish")
             {
                 Destroy(hit.transform.gameObject);  //works without calling enemyhealth script
             }
             */
             Debug.Log("Object Destroyed?");
         }
        // Debug.Log("Ray was shot.");  
     }
 }`

1 Reply

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

Answer by Seth-Bergman · Jan 05, 2013 at 11:10 PM

your code looks ok, but you can't access "gameObject" directly from the hit, instead use collider:

http://docs.unity3d.com/Documentation/ScriptReference/RaycastHit.html

 if (Input.GetMouseButtonDown(0)){       
     var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
     var hit : RaycastHit;

     if (Physics.Raycast(ray, hit)){
    if(hit.collider.tag == "prop")
         {           
           Destroy(hit.collider.gameObject);
      }
    }
 }

Note that for a Raycast to work, your object needs to have a Collider attached..

Comment
Add comment · Show 7 · 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 Devastator TF2 · Jan 06, 2013 at 02:17 PM 0
Share

Still Doesent Work after i used your script as a reference Seth/ Still Stuck :(

avatar image AlucardJay · Jan 06, 2013 at 02:28 PM 1
Share

Add a distance to your raycast. Also add a Debug to see what the raycast is hitting (it may be the correct object, but the tag may be incorrect) :

 if (Input.Get$$anonymous$$ouseButtonDown(0)){       
     var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
     var hit : RaycastHit;
     
     if (Physics.Raycast(ray, hit, 1000)){ // 1000 or $$anonymous$$athf.Infinity should be enough !
         // what did the raycast hit ?
         Debug.Log( "ray hit (name): " + hit.collider.gameObject.name);
         Debug.Log( "ray hit (tag): " + hit.collider.gameObject.tag );
         
         if(hit.collider.gameObject.tag == "prop")
         {           
             Destroy(hit.collider.gameObject);
         }
     }
 }

now when you raycast, you should get a message in the console window. Check that the tag is what you were expecting =]

avatar image Devastator TF2 · Jan 06, 2013 at 02:34 PM 0
Share

I dont get a debug message in the console. What am i doing wrong?

avatar image AlucardJay · Jan 06, 2013 at 03:01 PM 1
Share

Then as Seth stated, your objects need colliders to work. This may be a silly question, but is the code within a function?

Here is a test :

Create a new scene, put a cube in the scene. Attach this script to the cube (or the camera, or an empty gameObject, this is just a test). Hit play. Does the console now show Cube and untagged?

 function Update()
 {
     if (Input.Get$$anonymous$$ouseButtonDown(0)){       
         var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         var hit : RaycastHit;
         
         if (Physics.Raycast(ray, hit, 1000)){ // 1000 or $$anonymous$$athf.Infinity should be enough !
             // what did the raycast hit ?
             Debug.Log( "ray hit (name): " + hit.collider.gameObject.name);
             Debug.Log( "ray hit (tag): " + hit.collider.gameObject.tag );
             
             if ( hit.collider.gameObject.name == "Cube" )
             {           
                 Destroy(hit.collider.gameObject);
             }
         }
     }
 }
avatar image $$anonymous$$ · Jan 06, 2013 at 03:03 PM 0
Share

Your script is correct, it works for me. If you have tags and colliders set up on your gameobjects, this should definitely work. Can you provide some more info?

What exactly is the gameobject you're trying to hit?

Do you even get into the if (Get$$anonymous$$ouseButtonDown(0)) part of the code if you press the left mouse button?

Is that the only camera in your scene?

What if you try putting this code on your actual $$anonymous$$ainCamera, and use camera.ScreenPointToRay(Input.mousePosition); from that script?

Show more comments

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

13 People are following this question.

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

Related Questions

Raycast not changing value 0 Answers

I need help to make a Ray hit a 2D sprite hexagon map. 1 Answer

How can i detect 2 raycasthit2D in the same function storing their Vector2 position? 0 Answers

How to destroy ONE specific child object on touch. 1 Answer

2D raycast not working 0 Answers


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