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

Cone of sight

Discussion in 'Scripting' started by ostrich160, Sep 18, 2014.

  1. ostrich160

    ostrich160

    Joined:
    Feb 28, 2012
    Posts:
    679
    Hi guys, I was wondering if there is a way I can make a simple cone of sight, so that only what is infront of me can be seen. It doesnt need to do much, since its for the player to see things, not for AI, however, it cant be a gameobject (because its a multiplayer game and all players will have one) and it must detect and stop at objects. The game is obviously top down, but is 3d top down.
    Any advice?
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    I don't quite understand what you mean by a cone of sight for the player to see things... that sounds like what the Camera does naturally to me.

    If you just want to check whether an object is within so-many degrees of the player's forward direction, just use Vector3.Angle( transform.forward, theObject.transform.position - transform.position).

    HTH,
    - Joe
     
    Alekxss likes this.
  3. ostrich160

    ostrich160

    Joined:
    Feb 28, 2012
    Posts:
    679
    Look at something like Teleglictch for example, it means you can only see things with a certain angle, and the rest is blacked out
     
  4. MrPriest

    MrPriest

    Joined:
    Mar 17, 2014
    Posts:
    202
    Can't you just make a light source attached to the character's direction and position?
    I've never tried it, but it might be possible, rather than calculate yourself what is in and out of your field of vision...
     
  5. ostrich160

    ostrich160

    Joined:
    Feb 28, 2012
    Posts:
    679
    Im assuming you mean a spotlight.
    I just tried it, but it didnt really work. Aside from the fact it gave me more of a small circle of vision, besides its top down it also didnt stop at objects, meaning theres no ability to hide behind things like I wanted
     
  6. MrPriest

    MrPriest

    Joined:
    Mar 17, 2014
    Posts:
    202
    Well...
    You can add visibility to each object in the game, and unless it's in your sphere of vision, it's invisible (renderrer turned off)
    But I think it's quite heavy to do so.
    I honestly do want to know what solutions others come up with, though.
     
  7. ostrich160

    ostrich160

    Joined:
    Feb 28, 2012
    Posts:
    679
    I mean as I said its been done before so it must be possible, the issue with the renderer turned off issue is that I want the objects to be invisible so other players can hide behind them and such
     
  8. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    I think I'm starting to imagine what you're after, but I'm still not certain — can you point us to an example of what you're trying to achieve?

    Now I'm picturing something like a top-down view of a guy in a dark environment, with a flashlight. He can only see what his flashlight is pointing at, and so enemies can hide behind obstacles etc. Is that right?

    If so, I think a spotlight is the only way to go. The trick is, you have to set your camera to use Deferred Lighting rather than the standard Forward (or "Use Player Settings") rendering path. Check out the test scene below.

    You can see (because I've turned lighting off) in the scene view that there's a capsule behind the smaller block, but it's completely hidden in the shadows in the game view.

    HTH,
    - Joe
     
  9. MrPriest

    MrPriest

    Joined:
    Mar 17, 2014
    Posts:
    202
    He is talking about something like that:


    Looking at it again, I think they just made the walls continue in black up to the camera's pov.
     
  10. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Ah. Well games like that are typically done in 2D with raycasting. There's a neat explanation of this, and an amazing demo done entirely in HTML5/JavaScript on a Canvas (!), here.

    But in Unity, I think it would be much easier — and allow for some cool effects — to use a spotlight or point light, as demonstrated above. (I would have used a point light if I'd understood what he was after, but it's basically the same thing.) Just tweak the intensity and range until you're getting the look you want.
     
  11. ostrich160

    ostrich160

    Joined:
    Feb 28, 2012
    Posts:
    679
    Was literally just about to Priest, I guess the only difference is that in teleglitch you can see all around the room, just not past certain obstacles, where as I want it to just be a cone that stops on obstacles. And also that my game is 3d (top down still though). That HTML5 link is perfect, again minus the 360 view.
    The flashing analogy is perfect.

    The issue is with lights, is that I cant use deferred, since I dont have pro. Not only that, for when I play a spotlight down and tick to draw the halo, it just makes a light circle around my character from the top down, even if its angled to go forward
     
  12. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    You wouldn't point the spotlight down; you'd stick it inside your player object, pointing forward. Isn't the screen shot I posted pretty much what you want?

    But yeah, I guess not having Unity Pro may mean this option is closed to you. Ugh. That's going to make this very difficult to accomplish!
     
  13. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Well you could create a vertex shader which transfers the result from light to vertex alpha. In a lot of cases it would be an easy approx.

    But I still consider the method outlined in the above link to be pretty much spot on suitable for Unity.
     
  14. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Alternately, if you have Pro, use a camera to render the first-person perspective of the player to a RenderTexture and then use a post processing effect that translates the data rendered in the first person perspective back to the main camera's perspective and apply some kind of effect to it. This has the upside of not relying on lighting or colliders (raycasting).

    That's the technique used here: http://forum.unity3d.com/threads/released-gpu-line-of-sight-field-of-view.257324/
    Or you can just buy that straight away: https://www.assetstore.unity3d.com/en/#!/content/19764
     
  15. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    @KelsoMRK, thanks for pointing that out — that's super cool! I love that it runs on the GPU. What an elegant solution. Though since it requires Pro, it won't help @ostrich160, alas. Also, after playing with the demo a little, I notice that the other objects (circles) aren't really cut by the light cone as I would expect; instead they fade out, and sometimes you can see them a little even when they're outside the light cone. Maybe not a huge deal, but in a 3D game, I think using real lights may look better.

    @hippocoder, can you explain a bit more what you mean about the above method (ray-casting) being spot-on suitable for Unity? I don't quite see how it applies, unless you mean to apply the results of the ray tests to vertex colors, or otherwise hide objects that shouldn't be visible.
     
  16. ostrich160

    ostrich160

    Joined:
    Feb 28, 2012
    Posts:
    679
    Yeh I guess not having pro is gonna be tricky to get around here
     
  17. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    If you're just doing cone to see if something can be seen, then compare dot product to angle, and then if in cone, raycast for obstacles. This is the best trade off since dot product per enemy would be almost free, and you can also stagger it, plus the cast only happens if the dot product passes. For more information search Line of Sight Dot Product.
     
  18. ostrich160

    ostrich160

    Joined:
    Feb 28, 2012
    Posts:
    679
    Im not even doing it to see is somethings seen, its for the player not AI, so as long as it stops on objects in the way and can only see a cone, its working
     
  19. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Same thing.
     
  20. ostrich160

    ostrich160

    Joined:
    Feb 28, 2012
    Posts:
    679
    Damn, I thought my way would be easier