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

Question Lerping the Main Camera not working?

Discussion in 'Scripting' started by TheProcessYet_64, Jun 7, 2023.

  1. TheProcessYet_64

    TheProcessYet_64

    Joined:
    Jan 4, 2023
    Posts:
    39
    I have a first person camera that I'm using along with a script and when I do certain movements like crouching, I use Lerp to add a transition speed on my player position + scale so that my player crouches/stands over a very short amount of time rather than instantly. I've never had an issue with using Lerp but when it comes to doing the same thing with my camera, nothing happens.

    The issue with the code is, when I initially crouch, my camera instantly changes position. When I let go of the crouch key, the camera instantly reverts to its default position and then begins to Lerp from default position to crouch, which I don't understand.

    Code (CSharp):
    1. public Vector3 offset = new Vector3(0f, 0.779f, 0f); // Start offset
    2. public Vector3 newOffset = new Vector3(0f, -0.100f, 0f); // New camera position when crouching
    3. public Vector3 originalOffset = new Vector3(0f, 0.779f, 0f); // Revert back to start offset
    4. float lerpDuration = 1f;
    5.  
    6. public void Update()
    7.     {
    8.         if (Input.GetKey(KeyCode.LeftControl) && !isCrouching)
    9.         {
    10.             StartCoroutine(LerpCrouch());
    11.             isCrouching = true;
    12.         }
    13.         else
    14.         {
    15.             offset = originalOffset;
    16.             isCrouching = false;
    17.         }
    18.     }
    19.  
    20. IEnumerator LerpCrouch()
    21.     {
    22.         float timeElapsed = 0;
    23.         while (timeElapsed < lerpDuration)
    24.         {
    25.             isCrouching = true;
    26.             offset = Vector3.Lerp(offset, newOffset, timeElapsed / lerpDuration);
    27.             timeElapsed += Time.deltaTime;
    28.             yield return null;
    29.         }
    30.         offset = newOffset;
    31.     }
    I have changed GetKey to GetKeyDown and also the code inside the else statement but the results are the same.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,559
    Coroutines are not appropriate for this since conceivably two could be started simultaneously and fight each other in unpredictable ways.

    All you need is this value-smoothing pattern:

    Smoothing movement between any two particular values:

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

    You have currentQuantity and desiredQuantity.
    - only set desiredQuantity
    - the code always moves currentQuantity towards desiredQuantity
    - read currentQuantity for the smoothed value

    Works for floats, Vectors, Colors, Quaternions, anything continuous or lerp-able.

    The code: https://gist.github.com/kurtdekker/fb3c33ec6911a1d9bfcb23e9f62adac4

    EDIT: This character controller does not use coroutines for anything. It supports crouching and a lot of other features too: run, walk, jump, slide, etc.

    https://forum.unity.com/threads/a-basic-first-person-character-controller-for-prototyping.1169491/

    More on coroutines:

    Coroutines in a nutshell:

    https://forum.unity.com/threads/des...en-restarting-the-scene.1044247/#post-6758470

    https://forum.unity.com/threads/proper-way-to-stop-coroutine.704210/#post-4707821

    Splitting up larger tasks in coroutines:

    https://forum.unity.com/threads/bes...ion-so-its-non-blocking.1108901/#post-7135529

    Coroutines are NOT always an appropriate solution: know when to use them!

    "Why not simply stop having so many coroutines ffs." - orionsyndrome on Unity3D forums

    https://forum.unity.com/threads/things-to-check-before-starting-a-coroutine.1177055/#post-7538972

    https://forum.unity.com/threads/heartbeat-courutine-fx.1146062/#post-7358312

    Our very own Bunny83 has also provided a Coroutine Crash Course:

    https://answers.unity.com/questions...ected.html?childToView=1749714#answer-1749714
     
  3. TheProcessYet_64

    TheProcessYet_64

    Joined:
    Jan 4, 2023
    Posts:
    39
    Hey thanks for the reply. Sorry for the delay but I've been taking a look at your code plus trying to create my own, and I've made good progress however, after using debugging I've found an issue but I can't solve it.

    Code (CSharp):
    1. public Transform target;
    2. public float smoothTime = 0.25f;
    3. private Vector3 velocity = Vector3.zero;
    4.  
    5. void Update()
    6.     {
    7.         Vector3 crouchTargetPosition = target.TransformDirection(newOffset);
    8.         Vector3 standingTargetPosition = target.TransformDirection(originalOffset);
    9.  
    10.         if (Input.GetKey(KeyCode.LeftControl))
    11.         {
    12.             transform.position = Vector3.SmoothDamp(transform.position, crouchTargetPosition, ref velocity, smoothTime);
    13.         }
    14.         else
    15.         {
    16.             transform.position = Vector3.SmoothDamp(transform.position, standingTargetPosition, ref velocity, smoothTime);
    17.         }
    18.     }
    However, this line below overrides my SmoothDamp code.
    Code (CSharp):
    1. // Make the camera follow the player
    2.         transform.position = player.position + offset;
    If I take it out, SmoothDamp works but then I lose all mouse/camera movement. Debugging has helped, but I'm stumped on the solution.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,559
    Use a parented chain of transforms to add motions / rotations together.

    Turret aiming/rotating:

    https://forum.unity.com/threads/vector-lookat-unprecise.858511/#post-5658304

    Otherwise, just look at the link I provided above, which I will re-provide below:

    If you would prefer something more full-featured here is a super-basic starter prototype FPS based on Character Controller (BasicFPCC):

    https://forum.unity.com/threads/a-basic-first-person-character-controller-for-prototyping.1169491/

    That one has run, walk, jump, slide, crouch... it's crazy-nutty!!