Search Unity

How to initialize the rotation of cinemachine Free Look camera OnEnable()

Discussion in 'Cinemachine' started by joeyxiong1994, Jul 3, 2019.

  1. joeyxiong1994

    joeyxiong1994

    Joined:
    Apr 18, 2017
    Posts:
    20
    I have a third person game which used two set of camera, one is a FPS system like camera, and one is the free look camera of cinemachine. As I need to switch between two cameras, I simply just activate one set of camera and deactivate the other one when I click the aim key.

    Now I'm able to remember the Y rotation while the Freelook camera is on, and the FPS can inherit the Y rotation after switching from Freelook to FPS one. It's like after switching, my FPS can face to where my freelook camera faced to. However, the opposite way is not working as I don't know how to set up the free look camera's rotation while OnEnable(). Anyone can help me on this? I want to initialize the free look camera's rotation while OnEnable() or I want the free look camera to inherit the current Y rotation of my character's transform. Thanks
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,724
    Have you tried setting this in your FreeLook?

    upload_2019-7-3_9-52-14.png

    If your FPS is also a vcam (I hope so), then this will make the FreeLook auto initialize its rotation and Y axis when you transition to it, to minimize the distance the camera has to travel.

    Otherwise, you can manually calculate an appropriate angle to put in freeLook.m_xAxis.Value.
     
  3. joeyxiong1994

    joeyxiong1994

    Joined:
    Apr 18, 2017
    Posts:
    20
    Hi Gregory, thanks for your reply. I've tried to use two freelook camera to achieve this goal, like one camera is for far TPS view, one is for close FPS view. I put the cnimemachine freelook camera script part and camera part into one empty gameobject, and then I can switch between two camera by setting the parent gameobject to active or not. But after I selected the inherit position, after swtiching from two cameras, they still have its own rotations, which they do not inherit from each other. Could you please give me more hints about doing this?

    Below are the images of how I switch between two cameras and how they set up in the project hierarchy view.


    A minor question is the character rotation issue. As you can see from below image, I rotate my character to face the same direction as the camera's forward direction by using the transform.lookat. I noticed that if I move my mouse too quick to turn the camera left or right, my character somehow has a little bit delay to turn to the right direction. Is the lookAt bad at doing this? Like it took too much computations? Or it's just the freelook camera has a little bit damping or lerping time. Screen Shot 2019-07-12 at 02.32.36.png
    Screen Shot 2019-07-12 at 02.27.43.png Screen Shot 2019-07-12 at 02.27.50.png




     

    Attached Files:

  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,724
    aaahhh!! The problem is that you have 2 Cameras, with 2 brains. They are independent systems, and so the blending will not work properly. You should have only one camera, with one brain. Set it up like this, with 3 separate game objects, not parented to each other:
    • Main Camera (with CM Brain)
    • FreeLook1
    • FreeLook2
    Then, enable/disable the FreeLooks to select the active one. The main camera will track whichever one is active, and gracefully handle transitions when the active one changes.

    For the rotation question, it could be damping in the axis settings. Can you show an image of your FreeLook inspector?
     
  5. joeyxiong1994

    joeyxiong1994

    Joined:
    Apr 18, 2017
    Posts:
    20
    Thanks so much!!!! This works! And I wonder if I have a way to control the speed of this transition? It's a little bit slower than what I expect. I wish I can make it faster. And will I be able to send this event in script that I know it's in camera switching stage which I could disable my character's movement and mouseInput.


    And below are the inspector of one of my FreeLook. And the FreeLookAxisDriver is from another thread here that you provide me to control the damping of mouse input. In another thread, I asked that when I move mouse, I feel the camera is following a little bit late like having a lerp time, and this solves that issues. But camera follows correctly and rapidly responded by having this script, my character rotates like having a lerp time.

    Screen Shot 2019-07-12 at 08.30.05.png Screen Shot 2019-07-12 at 08.30.16.png Screen Shot 2019-07-12 at 08.30.24.png




     
  6. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,724
    The default blend time is controlled by this setting in the Brain:

    upload_2019-7-12_11-42-46.png

    You can also make specific blend times for specific transitions by creating a custom blend asset.

    You can poll brain.IsBlending if you want to disable input during a blend.

    If your camera is moving properly that's good. I don't know why your character is rotating less well. How are you animating it? Do you have a character controller script? Very likely that's where you should look.
     
  7. joeyxiong1994

    joeyxiong1994

    Joined:
    Apr 18, 2017
    Posts:
    20
    Hi, that blend works great! And I rotate my character by using transform.lookAt after calculating cam's forward direction to horizontal. Is the taking too much computation which results in this delay. Or cam_1.transform.forward is responded immediately after I move my mouse to rotate the camera?


    upload_2019-7-12_10-1-15.png






     
  8. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,724
    Not too much computation. It's very fast.
    Try taking the Main Camera's forward instead of cam_1
     
  9. joeyxiong1994

    joeyxiong1994

    Joined:
    Apr 18, 2017
    Posts:
    20
    Hi Gregory, I've tried to use the Camera.main and I put the below code in Update to debug. And I found there's a big angle between tranform.forward and the camera's forward while I'm rotating the camera horizontally just a little bit fast. Is this the limitation of using Cinemachine freelook camera as it has its own damp value somehow or lerp value? Or it's about my Update function that I set up not appropriately? I currently just put the rotation camera script in regular Update(). Could this be an issue?

    upload_2019-7-12_18-44-24.png



     
  10. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,724
    Yes, it's an ordering issue. The main camera is updated in CinemcahineBrain.LateUpdate, which happens after Update(), so you are using the previous frame's rotation.

    If you really want the transforms locked together then you can make the final adjustment to your character's rotation based on the camera rotation, after the camera rotation has been set. CM Brain issues an event (CameraUpdated) when the camera's position and rotation are set. You can hook into that, or you can do it in your script's LateUpdate, after setting its script execution order to be after CinemachineBrain.
     
  11. joeyxiong1994

    joeyxiong1994

    Joined:
    Apr 18, 2017
    Posts:
    20
    Thanks very much! I've done some research about the order of execution unity events but I still can't find the right way to let my LateUpdate wait for the CinemachineBrain's LateUpdate. Could you please provide me a code snippet to help me?




    upload_2019-7-14_11-47-36.png upload_2019-7-14_11-49-15.png

     
  12. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,724