Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by Pumpkinotto · Jun 23, 2016 at 04:53 PM · c#raycast

[C#] Raycast and raycasthit, destroy and instantiate gameobjects

Hello, i'd like to have a raycast that, when hits something, destroys it and instantiate an explosion prefab. here's my code

 Vector3 fwr = transform.TransformDirection (Vector3.forward);
         if (IsReadyToShoot == true && WantsToShoot == true) {
             WantsToShoot = false;
             Laser.SetActive (true);
             ASS.Play();
             Invoke ("Unloader", 0.1f);
             ReloadTime = 10f;
             if ((Physics.Raycast (Ray0.transform.position, fwr, 20f, Enemies)) || (Physics.Raycast (Ray1.transform.position, fwr, 20f, Enemies)) || (Physics.Raycast (Ray2.transform.position, fwr, 20f, Enemies)) || (Physics.Raycast (Ray3.transform.position, fwr, 20f, Enemies)) || (Physics.Raycast (Ray4.transform.position, fwr, 20f, Enemies)) || (Physics.Raycast (Ray5.transform.position, fwr, 20f, Enemies))) {
                 Debug.Log ("Hit!");
 
             }

as you can see i got 6 gameobject from which i cast 6 rays. i'd like to make a script that destroys every hit object that is hit from at least one ray, and to instantiata an explosion in the coordinates of the hit. i was trying to use raycasthit but even if i looked on the internet for long, i can't figure out how to make it work. thanks in advance

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

1 Reply

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

Answer by Habitablaba · Jun 23, 2016 at 05:07 PM

Use the version of Physics.Raycast that takes an "out RaycastHit hit" parameter. You'll also want to check each ray individually, so you'll be able to destroy more than just one object.

     RaycastHit hit;
     if (IsReadyToShoot && WantsToShoot ) {
         WantsToShoot = false;
         Laser.SetActive (true);
         ASS.Play();
         Invoke ("Unloader", 0.1f);
         ReloadTime = 10f;
         if ((Physics.Raycast (Ray0.transform.position, transform.forward, out hit, 20f, Enemies)) {
             Destroy(hit.gameObject);
         }
         if ((Physics.Raycast (Ray1.transform.position, transform.forward, out hit, 20f, Enemies)) {
             Destroy(hit.gameObject);
         }
         if ((Physics.Raycast (Ray2.transform.position, transform.forward, out hit, 20f, Enemies)) {
             Destroy(hit.gameObject);
         }
         if ((Physics.Raycast (Ray3.transform.position, transform.forward, out hit, 20f, Enemies)) {
             Destroy(hit.gameObject);
         }
         if ((Physics.Raycast (Ray4.transform.position, transform.forward, out hit, 20f, Enemies)) {
             Destroy(hit.gameObject);
         }
         if ((Physics.Raycast (Ray5.transform.position, transform.forward, out hit, 20f, Enemies)) {
             Destroy(hit.gameObject);
         }

Also: You can use transform.forward to get an object's forward vector, instead of trying to transform the world forward vector. You don't need to check "== true" for boolean values, you can just check the value

Hopefully this is enough to get you started?

Comment
Add comment · Show 1 · 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 Pumpkinotto · Jun 23, 2016 at 09:18 PM 1
Share

Thank you very much, you're an awesome person. Really clear and complete answer, good job. p.s. i noticed you're missing one parenthesis after each if p.p.s. Raycasthit does not support .gameObject directly you have to write .collider.gameObject to make it work

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

RayCast in WebGL not working 7 Answers

How do I move a raycast back on objects z axis? 1 Answer

Click On/Off Manager - C# 2 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