Search Unity

Free Camera Pan With Confiner

Discussion in 'Cinemachine' started by c1pr1an, Jun 17, 2019.

  1. c1pr1an

    c1pr1an

    Joined:
    Jul 4, 2014
    Posts:
    6
    Hello!

    I've set a confiner component to a virtual camera, and the bounding shape is a 2D polygon collider. There is no player to follow - instead, the player presses the WASD keys to move the virtual camera's transform around the map (hence it pans).

    This works. The camera will not go beyond the bounds of the world because of the confiner.

    The problem is that if the player presses the W key (go pan up), the transforms Y position of the virtual camera keeps going up even though the camera is hitting the collider bounds. Visually, this doesn't matter.

    However
    , if the player decides to pan down (pressing the S key), it will take a while before the camera can start moving down, because the Y position is fairly high since the player was holding the W key originally.

    Do you have any idea how I can fix this issue?
    Thanks!
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,728
    The confiner should also confine in the Y direction.

    One possible reason for it not working: you must make sure that the confining shape is large enough to contain the entire screen. If it's too small in the Y direction, for example, it won't confine properly.
     
    c1pr1an likes this.
  3. c1pr1an

    c1pr1an

    Joined:
    Jul 4, 2014
    Posts:
    6
    I think I didn't make myself very clear, let me try it again.
    The confiner works in all directions very good, I have no problem with it not restricting the movement of the camera.

    Short video with it in action:


    The problem I have is that, the camera transform have a different position then the cinemachine camera position when I drag the camera outside the confiner bounds (marked as yellow in the video) although they are the same exact object. And because of this, if I go too far off the bounds with the camera transform, when I try to get it back inside it creates a jerky movement to the camera because the cinemachine camera position still considers the transform to be way too far away from bounds.

    I guess the question is, can I set somehow the transform position to match the cinemachine camera position? Or is there a property for the actual cinemachine camera position that I can use?
     
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,728
    Yes, you can query the actual camera (the one with the brain) for its position and use that.
    Alternatively, you can look at vcam.State.CorrectedPosition, which is the same thing.
     
    c1pr1an likes this.
  5. c1pr1an

    c1pr1an

    Joined:
    Jul 4, 2014
    Posts:
    6
    Oooh, I thought it's something more complicated behind, it never occurred to me that's actually Main Camera's position. It makes a lot of sense now.

    Thank you very much for your response, I really appreciate it!
     
    Gregoryl likes this.