Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Have raycast ignore object once it's hit (not using layers)

Discussion in 'Scripting' started by crash664, Feb 8, 2016.

  1. crash664

    crash664

    Joined:
    Nov 22, 2012
    Posts:
    91
    Hi all,

    I have a second camera in the scene which I am using as a "view finder" to take photos of a scene on an action. What I want to do is raycast every pixel of the photo as it's taken using this camera as the origin and return hit info of some specific objects.

    I can use layer masking to make it so the ray will only hit those objects, but, what I would like to do in order to reduce inefficiency (the time it takes to cast the ray against every single pixel), is have the ray ignore the objects once they have been hit once already.

    Does anybody know of a way?

    Thanks!
     
  2. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
    What's it matter whether you make the camera ignore the object though, you are shooting just as many rays as before, and even worse the object behind the ignored object would be detected even if it is not seen in the camera view.
     
  3. crash664

    crash664

    Joined:
    Nov 22, 2012
    Posts:
    91
    Yes, but it's far more efficient to only cast one ray for each object detected.

    What I ended up doing is once the object is hit the first time, I change that objects layer while I ray cast against the "picture", then it has one less object to worry about colliding with, which means a potentially large reduction in operations in the portion:

    Code (CSharp):
    1. if (Physics.Raycast(...))
    It certainly helped. But because I'm doing this in VR, I'm going to have to throw it all into a coroutine I think so it can carry on analysing in the background.

    Thanks!
     
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    You can raycast against specific colliders. Don't know if this helps.
     
  5. crash664

    crash664

    Joined:
    Nov 22, 2012
    Posts:
    91
    Do you mean using layers?
     
  6. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    crash664 likes this.
  7. crash664

    crash664

    Joined:
    Nov 22, 2012
    Posts:
    91
    Well spotted, you're right. I think that it entirely applicable!