Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Setting Cinemachine FreeLook Axis Values Through Code

Discussion in 'Cinemachine' started by Nerdin8or, Jan 22, 2019.

  1. Nerdin8or

    Nerdin8or

    Joined:
    Feb 19, 2018
    Posts:
    7
    Hello everyone,

    I'm creating an isometric strategy game in 3D space. I use a Cinemachine FreeLook camera to pan and rotate the battle field, but since it's also supposed to be pseudo-isometric, I wanted a quick way to snap to 45-degree increments.

    I've got it almost entirely working, but the problem I'm running into is that when the player is trying to snap from 315 degrees to 0 or vice versa, the camera spins all the way back around the other direction instead of smoothly wrapping in the correct direction.

    I think this is because I'm using a Mathf.Lerp in LateUpdate to rotate the camera toward the Target Angle. Since the new target angle is almost 360 degrees away, it doesn't know how to handle the wrapping rotation.

    Wrapping works just fine when using the joystick inputs; it just can't handle me trying to manually set axis values. Is there any method within the FreeLook class that could handle this better? Or does anyone know of a better solution to allow that smooth wrapping?

    Thank you!
     
    WanSCAD likes this.
  2. Nerdin8or

    Nerdin8or

    Joined:
    Feb 19, 2018
    Posts:
    7
    I actually solved it myself! Instead of using a Mathf.Lerp function, I used Quaternion.Lerp:

    void LateUpdate() {
    if (doSnapCamera) {
    //Basically, create two new Quaternions with the values in the y and let Quaternion.Lerp handle it
    freeLook.m_XAxis.Value = Quaternion.Lerp(Quaternion.Euler(0, freeLook.m_XAxis.Value, 0), Quaternion.Euler(0, targetAngle, 0), 5 * Time.deltaTime).eulerAngles.y;
    if (Mathf.Abs(freeLook.m_XAxis.Value - targetAngle) < 0.1f) {
    doSnapCamera = false;
    }
    }
    }


    That works correctly, presumably because Quaternions innately know how to wrap 359 straight to 0.

    It may still be too messy, since I'm not sure how expensive it is to use the Quaternion.Lerp function in this way--if anyone knows a better solution, please let me know!
     
    WanSCAD and NateReese77 like this.
  3. GodModeTech

    GodModeTech

    Joined:
    Jun 21, 2010
    Posts:
    60
    I don't think Quaternion lerp is expensive. Go with it.
     
  4. TJHeuvel-net

    TJHeuvel-net

    Joined:
    Jul 31, 2012
    Posts:
    838
    He went with it one year and a half ago
     
    sniffle63, soumilrathi and homemacai like this.
  5. Confused_potato

    Confused_potato

    Joined:
    Mar 27, 2021
    Posts:
    1
    I've been looking for something like this for so extremely long, thank you!
     
  6. AlexDemon01

    AlexDemon01

    Joined:
    May 2, 2019
    Posts:
    1
    This was super helpful, THank you!!!