Search Unity

Virtual Camera's transform going too far

Discussion in 'Cinemachine' started by ScuffedCD, Sep 17, 2021.

  1. ScuffedCD

    ScuffedCD

    Joined:
    Feb 4, 2020
    Posts:
    41
    I've spent hours trying to figure this out with no results.

    Basically I'm making a 2D top down game and I want the player to left mouse click and drag to go around the map. I'm doing this by moving the Virtual Camera's transform when they hold down left mouse.

    The map has a polygon collider around it that does successfully stop the camera view from going out of bounds, but the camera's transform continues to keep going. This causes the game to take a long time to move away from the boundary, since its waiting for the transform to return back.

    I've attached a picture that shows the problem. The camera's transform goes wayy past the polygon collider (yellow box). Since its so far, it takes awhile to come back into the fov (red box) and it just looks like the camera is stuck/delayed.

    The best method I could think of is that the camera's transform is ALWAYS in the center of the field of view, and if the FOV can't go any farther, the transform locks in place. If anyone knows how to solve this issue, or just a better script.
     

    Attached Files:

  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,728
    I assume that it's the vcam's transfom that goes where you drag it, even outside the bounds. The main camera's transform, however, will stay inside the bounds. This will happen if you are using a CinemachineConfiner on the vcam.

    Try initializing the vcam's transform each frame with the main camera's position, then apply the mouse movement.
     
  3. ScuffedCD

    ScuffedCD

    Joined:
    Feb 4, 2020
    Posts:
    41
    If I keep moving the Vcam's position to the Main camera, then how will I be able to move the camera around the map? The main camera's coordinates can't be changed during play mode, even if I change them in inspector or use the move tool. It's position is always stuck to the same as the virtual cam
     
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,728
    I don't quite understand what you're doing. With the CM paradigm, you never move the main camera. Instead, you just move vcams, and the main camera will track the current active one. I assume that you're moving a vcam around, but because it has a confiner, its transform doesn't reflect the true position of the main camera. That's why I'm suggesting using the main camera's transform as input to your vcam move code. Still move the vcam, but use the main camera's transform to find the actual camera position.

    Alternatively, instead of using the main camera's transform, you can get the actual camera position from the vcam: call vcam.State.GetCorretedPosition() and vcam.State.GetCorrectedOrientation(). This will include the effects of the confiner.