Search Unity

Change Cinemachine camera start position

Discussion in 'Cinemachine' started by eliasjgnilsson, Dec 14, 2021.

  1. eliasjgnilsson

    eliasjgnilsson

    Joined:
    Mar 1, 2021
    Posts:
    77
    Background
    I have a Cinemachine free look camera in a prefab, amongst other stuff. When loading the scene I instantiate the prefab (with the camera as a child) and set the Follow and LookAt target when instantiated.

    Problem
    When loading the scene the camera now starts from some random position in the ground and then smoothly moves to its target where it then starts to work perfectly. However, I want the camera to start from a given position above the map and then move it toward the target.
    How can I change where the Cinemachine camera starts at?
     
    MaxLohMusic likes this.
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,719
  3. eliasjgnilsson

    eliasjgnilsson

    Joined:
    Mar 1, 2021
    Posts:
    77
    Can you please give me some details on how to use this? Doc says I can use CinemachineVirtualCameraBase. ForceCameraPosition but that class doesn't exists. So I should create this function myself and then call it with the desired pos and rot? And in the function I need to set transform pos and rot to given values?
     
    MaxLohMusic likes this.
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,719
    FreeLook and Virtual camera both inherit from CinemachineVirtualCameraBase, so you can just call it directly on your FreeLook, i.e.
    myFreeLook.ForceCameraPosition(desiredPos, desiredRot)
     
  5. eliasjgnilsson

    eliasjgnilsson

    Joined:
    Mar 1, 2021
    Posts:
    77
    Thanks, it is not working though. I have an empty gameobject which I set where I want the camera to be starting. Then I have the following code on the camera (or on some other object, it was the same result):

    Code (CSharp):
    1. cinemachineCamera.ForceCameraPosition(cameraStartPos.transform.position, cameraStartPos.transform.rotation);
    Then later on I set my target and lookAt to my player:

    Code (CSharp):
    1. cinemachineCamera.m_LookAt = _target.transform;
    2. cinemachineCamera.m_Follow = _target.transform;
    It is still starting in this random middle position though when loading the scene and nothing is changing.
     
  6. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,719
    When you set the LookAt/Follow targets, you are enabling the procedural tracking code that positions the FreeLook instantly, relative to the player, according to the tracking algorithm. That overwrites the position you initially forced.

    Try instead an alternative approach. Create an ordinary virtual camera at a lower priority. Position it at the desired start camera position. That can serve as your default camera, active before the FreeLook is instantiated. Then, when you instantiate the FreeLook (at a higher priority) the CM Brain will detect its presence and activate it, causing the camera to blend from the start vcam to the FreeLook's position. Is that the effect you're looking for?
     
  7. eliasjgnilsson

    eliasjgnilsson

    Joined:
    Mar 1, 2021
    Posts:
    77
    Good idea, this should work great!
     
  8. Chris_Payne_QS

    Chris_Payne_QS

    Joined:
    Jul 16, 2016
    Posts:
    84
    I have a similar problem. I have a ResetCamera function that snaps the FreeLook camera behind the player using ForceCameraPosition(), which works fine during play. But at level start the level the Cinemachine rig is initialised at the origin and then reset into position (because the spawn point is in an additive level). Even though I am using the same function, it does not work at level start, instead causing the camera to fly across the level.

    As far as I can tell there are no blends in progress when the ResetCamera is called. Even turning the CinemachineBrain off and on again (with a few frames delay) doesn't prevent this blend. Is there any way I can flush the system to ensure the camera just snaps to where it should be before I fade in?

    I don't want to use Cut as the default blend because that will make all my other camera transitions cut instead of blending, unless I create and maintain blend rules for every camera in the game (Ideally I'd register a callback to tell the system what type of blend to use but IDK if that is even the problem here)
     
    samol2014 likes this.
  9. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,719
    When you assign a new Follow target or move the existing target, then the camera won't know to snap (it will think that the target just moved quickly, and will use damping to follow it). You can try setting freeLook.PreviousStateIsValid = false to cancel the damping.

    You can do that by setting the CinemachineCore.GetBlendOverride delegate.
     
    samol2014 likes this.
  10. EaglemanGameDev

    EaglemanGameDev

    Joined:
    Aug 29, 2020
    Posts:
    6
    Did this solve your issue? I am stuck also.
     
  11. siliconkgk

    siliconkgk

    Joined:
    Oct 5, 2021
    Posts:
    27
    There seems to be an issue with the suggested solution from Unity. I'm running into the same issue. I am using a virtual camera that is set to position itself behind the player but always starts looking at the side of the player on scene start, then as the player moves forward it rotates to behind the player.

    Where is the virtual camera getting its initial rotation set from because the only options I see we can edit are the ones that effect the cameras movement during play. I'm not sure where its deciding that it initially should view the side of the player before the player starts moving. Creating multiple cameras to solve a starting camera orientation issue cannot be the solution.
     
  12. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,719
    You didn't show the vcam inspector but it sounds like you have the BindingMode set to SimpleFollowWithWorldUp. In that binding mode, the camera makes no effort to go "behind" the player, it just moves as little as possible from its current position in order to maintain the desired distance (as though being pulled by the player with a string). When you instance the vcam, you can just set its position to where you want it, otherwise it will just show up wherever you happened to author it. You don't need a separate vcam for your usecase.
     
    siliconkgk likes this.
  13. Turbo822

    Turbo822

    Joined:
    Jun 23, 2022
    Posts:
    1
    Hi. I wanted to press "C" to move the CinematicmachineFreeLook camera to a certain position and press "C" again to return to my position ... The problem is that I can't move with CinemacitcFreeLook or the main camera at all. Don't know how to solve this?
     
  14. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,719
    I would instantiate a second vcam at the desired position and activate it to let the CM Brain blend the camera there. When you deactivate it, the camera will blend back to the original position.