Search Unity

smooth follow camera help

Discussion in 'Scripting' started by Tegleg, May 9, 2019.

  1. Tegleg

    Tegleg

    Joined:
    Jun 10, 2013
    Posts:
    79
    hello

    i dont normally need assistance with coding but this one is doing my head in.
    all im trying to do is have a very simple third person camera with a smooth 'lag', simple right? apparently not.
    Code (CSharp):
    1.  LateUpdate()
    2. // get a position behind the player
    3. Vector3 wantedPosition = target.transform.position - (target.transform.forward * distance);
    4. //move it up a bit
    5.  wantedPosition.y = target.position.y + height;
    6.  
    7. //lerp from current camera position to wanted position
    8. transform.position = Vector3.Lerp(transform.position, wantedPosition, smooth);
    the code above produces a smooth movement from the initial camera position to the 'wanted position'.
    but once the camera has reached that position it sticks to it like glue. no matter what the player is doing, how quickly it moves or rotates, the camera rigidly sticks to that position as if there is no lerp at all.

    i have tried with various lerp type things, MoveTowards, SmoothDamp etc and they all produce the same result.
    what am i missing?
    thanks
     
  2. Dextozz

    Dextozz

    Joined:
    Apr 8, 2018
    Posts:
    493
    How often are you calling this piece of the code? If you are calling it only once the Lerp function will only move to the wanted position only once and stay there. Could we get a bigger part of the script (an entire method would be nice) so we can have more information?
     
  3. Tegleg

    Tegleg

    Joined:
    Jun 10, 2013
    Posts:
    79
    thanks for the reply.
    im calling it in LateUpdate(), also tried Update()

    the only things i missed out was
    smooth * Time.deltatime
    and there is a LookAt(target) called after the Lerp

    im keeping it as simple as possible as its just a prototype atm
     
  4. Tegleg

    Tegleg

    Joined:
    Jun 10, 2013
    Posts:
    79
    As tradition dictates i will answer myself.

    the problem was that the camera was a child of the player. for some unknown reason this produced the results i was seeing.
    the answer was to remove the camera from its parent like this
    Code (CSharp):
    1. Cam.transform.parent = null;
    thanks
    your welcome