Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Question CinemachineFreeLook component - rotate towards given target

Discussion in 'Cinemachine' started by Micuda, Apr 10, 2024.

  1. Micuda

    Micuda

    Joined:
    Nov 11, 2016
    Posts:
    14
    Hello all! I am using Cinemachine to move the camera in my 3D mobile game. I am using CinemachineFreeLook component to achieve a behaviour when the camera rotates around the player - player object is set up to both Follow and Look at targets.

    This works fine, but now I would like to make it work in the sense that the camera will automatically rotate towards given Vector3 position in the scene, while maintaining player in the center of the camera (adding illustrative picture below, basically camera needs to stay on it's rig). I know I need to achieve this by setting up correct CinemachineFreeLook.m_XAxis.Value, but I just cannot figure out how can I get the desired angle. Currently the X axis value range is set to be wrapped between 0 and 360 degrees on the Cinemachine component. Can some of you please help with this? Thank you in advance!

    NOTE: I don't need explanation regarding how to do whole lerping towards particular X angle in Update, I already have the functional code for camera lerp. I am solely interested on how to get the needed target angle.

    camera rig.png
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,843
    What is the Binding Mode of your FreeLook?
     
  3. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,598
    Micuda, the m_XAxis thing seems like it should be an easy two-way controllable thing, but if you watch the Inspector while it's running, it appears to stay at zero even if you increase or decrease it. This is apparently tied to a delegate function in the orbital transposer, but it's not obvious from my reading of the code or docs how you provide your own delegate or specify a heading for the built-in delegate to use. There's a ForceCameraPosition() method but in my experience it's prone to glitching with "look rotation zero" errors. In the end, I position the virtual camera myself and then set the m_InheritPosition back to true.
     
  4. Micuda

    Micuda

    Joined:
    Nov 11, 2016
    Posts:
    14
    It is set to "World Space"

    I am actually able to see the value changing in the inspector (CinemachineFreeLook->Axis Control->X Axis->Value) and I am able to manipulate it's value through the code by directly assigning cmFreeLookComponent.m_XAxis.Value, thus I can change camera X axis position. Maybe it is somehow tied to overall component settings...
     
  5. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,843
    Excellent, that makes it easier. The value of the X axis in this case is just the world-space yaw angle of the camera. So you would need to find the angle between world +Z and the line connecting the player to the desired LookAt target (projected onto the XZ plane). That would be the value to put in the X axis.
     
  6. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,843
    The Binding Mode governs the way the X axis value is interpreted. If it's Simple Follow with World Up, then it's in Camera Space (i.e. the angle is relative to the camera forward axis). So it must always be 0 because the camera is never looking in a direction other than its forward axis. The value may change momentarily during a frame (when you move the mouse, a new value gets put there) but by the end of the frame the camera gets positioned accordingly and the value reverts to 0.