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
0
Question by JungoLeo · Oct 20, 2018 at 12:03 PM · raycastcolliderarrayoverlapsphereline of sight

Destroy objects within ordered Array by line of sight

Hello,

the code i provided works to the extent, that it rightfully destroys an object if it has the breakable tag and not if it has the unbreakable tag, it also protect objects with the breakable tag behind unbreakable tag objects(as intended).

I want objects in range who have the breakable tag to be destroyed even if they are behind each other(so objects with the breakable tag dont protect the object behind them), i thought ordering them and than just deleting them from closest to farthest would achieve that, but it sadly doesnt.

Any idea why that is the case?

   Vector3 spawnPos = transform.position;
         float radius = 30f;
 
         Collider[] hitColliders = Physics.OverlapSphere(spawnPos, radius, levelMask);
         hitColliders = hitColliders.OrderBy(point => Vector3.Distance(spawnPos, point.transform.position)).ToArray();
         int i = 0;
         while (i < hitColliders.Length)
         {
 
             RaycastHit hitPoint;
             Vector3 dir = hitColliders[i].transform.position - spawnPos;
             Debug.Log(hitColliders[i].transform.position);
             if (Physics.Raycast(transform.position, dir, out hitPoint))
             {
                 Debug.Log(hitPoint.collider.gameObject.name);
                 if (hitPoint.collider.tag == "breakable")
                 {
                     Destroy(hitPoint.transform.gameObject);
                 }
                 if (hitPoint.collider.tag == "unbreakable")
                 {
                     //dont do anything
                 }
             }
             i++;
         }
Comment
Add comment · Show 5
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 hexagonius · Oct 20, 2018 at 09:43 PM 0
Share

why are you sorting the result of the order does not matter?

avatar image JungoLeo hexagonius · Oct 21, 2018 at 11:01 AM 0
Share

I dont understand the question? why am i sorting the collider[]? in order to destroy the objects one after another from closest tp farthest.

avatar image hexagonius JungoLeo · Oct 21, 2018 at 09:13 PM 0
Share

this is not a coroutine, right? and every breakable found should be destroyed anyway. so why not iterate and destroy them without sorting?
get all, iterate, check for breakable, destroy...

Show more comments
avatar image DawidNorasDev · Oct 22, 2018 at 12:46 PM 1
Share

alt text

This is an example, where basically sorting by distance won't work, because while trying to destroy red object, you would find green one

objects.png (11.4 kB)

1 Reply

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

Answer by Esteem · Oct 22, 2018 at 03:56 PM

the Destroy() call queues the object for destruction (and OnDestroy() is called as the last thing in this object's existence which means it's possible to hit an object that you've Destroyed, because it's still there in that same frame.

When you raycast against an object that's destroyable you mark it for destruction and then you raycast against objects behind it but it hits the same object again, marking it again

\\

Edit.: Adding context and sauce:

"Actual object destruction is always delayed until after the current Update loop"

Unity docs for Object.Destroy()

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

161 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 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 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 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 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Don't allow raycasts to go through colliders? 3 Answers

OverlapSphere returned collider arrays 1 Answer

OnCollisionEnter-issue 1 Answer

Enemy line of sight using linecast and colliders 1 Answer

Moving objects using raycasting? 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