• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

Smooth Movement on Geometry Collission? 0 Answers

How to determine which part of an NPC is hit? 1 Answer

How to block dragging if collider hits 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges