Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Let us know a bit about your interests, and if you'd like to become more directly involved. Take our survey!
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Switch camera if player is on sight

Discussion in 'Cinemachine' started by BenouKat, Apr 13, 2018.

  1. BenouKat

    BenouKat

    Joined:
    Feb 29, 2012
    Posts:
    210
    Hi !

    I'm trying to do (what I think) a simple behvaiour with Cinemachine. I have multiples camera in my scene, some are static, some are on a dolly track.

    I want them to go live if the player is seen by the camera. If there's multiple camera that see the player, the priority of each cam decide which of them is rendering.

    I tried to play with "Clear Shot" component, and Virtual Camera Collider, but it seems that there's only a decision "by distance", and not "if it's on sight".

    Is there a way to achieve this ?
    Other question : If there's no Cinemachine behaviour that can do that, can I control which virtual cam is live in code ? I didn't find any function in doc that (I can do the camera manager myself, just don't want to lose all the magic blend, etc).

    Thank you !
     
  2. Carwashh

    Carwashh

    Joined:
    Jul 28, 2012
    Posts:
    303
  3. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    1,637
    Just to clarify: the Collider does in fact rate the shots according to whether the target is in sight. Perhaps this is not obvious, as thee are no settings for it in the inspector. The distance ranking is an optional feature, hence the setting.
     
  4. BenouKat

    BenouKat

    Joined:
    Feb 29, 2012
    Posts:
    210
    Oh yes, actually I didn't managed it without have "Camera zone" where I have to disable / enable some camera when entering a zone. My goal was to have 0 line of code using this but it seems I don't understand perfectly how it works yet.

    My situation : I have multiple type of camera on my level. Some are static, some are on a dolly track, and one, the "main", is hard following the player.
    The behavior I try to have is :
    - If the player is seen by a static OR a dolly tracked camera, the camera with the highest priority go live.
    - If the player is not seen by any camera OR by a camera that is too far, the "main" hard following camera go live.

    I didn't find a complete tutorial to understand how the Cinemachine Collider works, just the Unity one.

    I've seen all the parameters to not have a camera colliding with the wall : I understood, I've seen the parameters to evaluate the optimal distance from the target : I understood.
    But I didn't find a parameter like the one called "Preserve light of sight", as I've seen it in old Unity tutorial videos. In fact : When I was trying to play with the optimal distance with a setup of some cameras, but here's the problem I faced :

    - I have a room with a static camera
    - My player gets outside the room, and a dolly tracked camera starts to follow him outside
    - If the player goes too close to the outside wall of the room, the room's camera says "hey ! My optimal distance is better, let's go live !", but the player is not in sight, he's at the other side of the wall.

    So I was wondered if "on sight" was done without testing if the player is beyond a wall or something blocking the view ? But since it's called Collider it was clear to me that there was some raycast test to check this case. I've hard time finding answers. :(

    Am i missed something ?

    Just to add image to this long post : This is the thing I've accomplished with my "camera zone" system. It will help to understand what type of camera behaviour I'm trying to have without write a single line of code :


    Thanks a lot ! :)
     
  5. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    1,637
    What you are describing should be possible with ClearShot.

    The tutorial was outdated, in the new version "Preserve Line Of Sight" has been renamed to "Avoid Obstacles". If you enable this, the cameras will be moved if there is an obstacle, until the target can be seen again. For your application, probably you should not have it on for most of the cameras. You might want to have it on for the follow cam.

    It's possible that the wall you're talking about is being ignored by the collider. It will ignore obstacles that are too close to the target. This distance is defined by the "minimum distance from target" param.

    upload_2018-4-19_13-28-23.png

    Also look at "collide against" layer mask. Obstacles not on these layers will be ignored.
    Also, obstacles tagged with the "ignore tag" will be ignored. You will want to set this to the target's tag, otherwise the target itself may be considered to be an obstacle.
     
    SFvdB likes this.
  6. SFvdB

    SFvdB

    Joined:
    Mar 10, 2014
    Posts:
    7
    Thank you. This is the little bit of information I needed to get my cameras set up properly!