Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How do I go about smoothing out the movement of the CameraRotationPoint object I have in my scene?

Discussion in 'Scripting' started by Danteb132, Jan 26, 2021.

  1. Danteb132

    Danteb132

    Joined:
    Jan 26, 2021
    Posts:
    8
    I have an object parented to the character that is the anchor for what the Camera is supposed to look at. I have the character change direction when I press the key and of course the CameraRotationPoint anchor moves with the character, but sometimes during movement it snaps immediately and other times it is smooth.

    Heres the video of the problem.


    What I would like to do is to Lerp the movement of the Character to make the turning completely smooth, and so that the CameraRotationPoint isn't snapping along with the Character, but I'm unsure how to do this.

    Code (CSharp):
    1. private void Update()
    2.     {
    3.         //WALK AROUND CODE
    4.         float mH = Input.GetAxis("Horizontal");
    5.         float mV = Input.GetAxis("Vertical");
    6.  
    7.         Vector3 movement = new Vector3(mH, 0, mV);
    8.  
    9.         cc.Move(movement * Time.deltaTime * playerSpeed);
    10.  
    11.         if (movement != Vector3.zero) {
    12.             transform.forward = movement;
    13.         }
    14.        
    15.     }
    Thats that code for the walking around and the if statement is making it turn, and I don't know how to lerp it so that its smooth. Thanks for any help, and I can provide any other code needed even though there isn't very much more.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,336
    This is a decent approach. You can do this trivially by keeping the notion of where the camera is looking now, versus where the above lookpoint is, and tweening the now point smoothly to the desired point.

    Here's some examples that do it with scalars (floats) but the same exact process can be done with Vectors:

    Smoothing movement between discrete values:

    https://forum.unity.com/threads/beginner-need-help-with-smoothdamp.988959/#post-6430100

    The code: https://pastebin.com/ePnwWqnM

    ALTERNATIVELY, you could look into Cinemachine because I think it can do this sorta thing already and there's probably more controls on how the gaze change works. Up to you.