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

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

Have you tried setting this in your FreeLook?

4710080--445055--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.

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. 4739240--449444--Screen Shot 2019-07-12 at 02.32.36.png
4739240--449438--Screen Shot 2019-07-12 at 02.27.43.png 4739240--449441--Screen Shot 2019-07-12 at 02.27.50.png

4739240--449447--upload_2019-7-12_2-33-27.png

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?

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.

4740305--449735--Screen Shot 2019-07-12 at 08.30.05.png 4740305--449738--Screen Shot 2019-07-12 at 08.30.16.png 4740305--449741--Screen Shot 2019-07-12 at 08.30.24.png

The default blend time is controlled by this setting in the Brain:

4740356--449750--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.

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?

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

Not too much computation. It’s very fast.
Try taking the Main Camera’s forward instead of cam_1

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?

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

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.

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?

4745384--450374--upload_2019-7-14_11-47-36.png4745384--450377--upload_2019-7-14_11-49-15.png