Search Unity

Multidirectional Raycast?

Discussion in 'Scripting' started by DiamondWolf2, Aug 7, 2019.

  1. DiamondWolf2

    DiamondWolf2

    Joined:
    Dec 9, 2018
    Posts:
    16
    I'm trying to make a system where a room will do something when the player can see it. However, the camera system means the player can see in all directions simultaneously. How would I go about doing this?
     
  2. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    What do you mean you can look at all direction at once?, a 360 view?
     
  3. DiamondWolf2

    DiamondWolf2

    Joined:
    Dec 9, 2018
    Posts:
    16
  4. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    So the room is invisible only from being ocluded by something.

    You can try raycasting to each of its corners, if one hits it it's visible.

    If you provide more info about the enviroment we can help better.

    BTW - how and what are you doing with a 360 view?
     
  5. DiamondWolf2

    DiamondWolf2

    Joined:
    Dec 9, 2018
    Posts:
    16
    The room is fairly complex, but has a relatively small entrance to check through.

    Before the room's renderer is enabled: (the renderer is meant to be enabled when the room is seen)


    After: (currently using trigger object)


    Something to note: The 360 view is what the player object (yellow sphere) "sees"; the actual camera is detached, as was used to take the screenshots.

    Ignore the excessive gray. I'm still tweaking ambient lighting.
     
    Last edited: Aug 8, 2019
  6. Cyber-Dog

    Cyber-Dog

    Joined:
    Sep 12, 2018
    Posts:
    352
    This would probably be more efficiently achieved by collision triggers.
    Have a child object with a collider and script attached to each "scene module", and in the on trigger enter function enable the parent render's.
     
  7. DiamondWolf2

    DiamondWolf2

    Joined:
    Dec 9, 2018
    Posts:
    16
    That's what I do currently, but that requires either that I spend an eternity calculating where the trigger should go to accurately represent the line of sight or remove the sight check entirely, waiting for the player to enter the area.

    I did however find a way to cut this down to a small handful of targeted linecasts, targeting the center and each corner of a trigger box in the room's entrance. If any do not hit level geometry, then the room is most likely in sight. It's not foolproof, but it works well enough for my purposes.

    It still would be nice to know if what I was originally thinking (an object raycasting in a full sphere around it, or a similar method to determine possible line of sight if the object then became oriented correctly) is possible however.
     
  8. Cyber-Dog

    Cyber-Dog

    Joined:
    Sep 12, 2018
    Posts:
    352
    Ok I get ya. Well here is a though. Considering you are gonna be raycasting every physic's frame. It wouldnt be any more intensive to have the ray spinning in a circle.. Like if you have 360 rays going, thats were it would be intensive..

    So you can store your rays direction variable in a vector, and every frame rotate by 1 degree. It would still fire fast enough that you wouldn't even notice its not actually firing 360 rays each time.

    Off the top of my head you should be able to get away with just something like this..

    Code (CSharp):
    1. dir = new vector3(0,0,(dir.y == 359) ? 0 : diry++);
    And if you are noticing some latency with the turning, just increment by say 10 instead of 1.
     
    Last edited: Aug 9, 2019
  9. DiamondWolf2

    DiamondWolf2

    Joined:
    Dec 9, 2018
    Posts:
    16
    That might work.
    Another thought to consider: How would you expand it to a sphere rather than a circle?
     
  10. Cyber-Dog

    Cyber-Dog

    Joined:
    Sep 12, 2018
    Posts:
    352
    The same concept, just adjust the x and y values as well. You can do a Debug.DrawRay to preview how it would look like.