Search Unity

camera behavior and lerp damping

Discussion in 'Scripting' started by Jean-Fabre, Sep 13, 2007.

  1. Jean-Fabre

    Jean-Fabre

    Joined:
    Sep 6, 2007
    Posts:
    429
    Hi,

    I am trying to use lerp on a camera position to seek it's target but with no great result, it isn't fluid when seeking fast, slow lerp is ok tho. it's as if I was using Update() rather than LateUpdate or something like that:

    I am currently doing the race tut ( to give you an idea of the environment I am in. and this is a simple alteration of the /1 Basic Follow Camera/camera.js

    Here is the code.

    Code (csharp):
    1.  
    2. var target : Transform;
    3. var distance = 10.0;
    4. var height = 5.0;
    5.  
    6. function LateUpdate () {
    7.     // Early out if we don't have a target
    8.     if (!target)
    9.         return;
    10.    
    11.     // Get the forward direction of the target
    12.     forward = target.TransformDirection (Vector3.forward);
    13.  
    14.     newPosition = target.position - forward * distance + Vector3(0,height,0);
    15.  
    16.  
    17.     transform.position = Vector3.Lerp(transform.position, newPosition, 0.8*Time.deltaTime);
    18.    
    19.  
    20.     // Always look at the target
    21.     transform.LookAt (target);
    22. }
    23.  
    damping with 0.8 increase the fluidity but higher value increases the unwanted effect...

    thanks for your help,


    Jean
     
  2. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    If I understand what you are doing correctly then there is just one small mistake in your code. Instead of
    Code (csharp):
    1. transform.position = Vector3.Lerp(transform.position, newPosition, 0.8*Time.deltaTime);
    ...you probably want
    Code (csharp):
    1. transform.Translate((newPosition - transform.position) * (0.8 * Time.deltaTime), Space.World);
    (That is to say, you don't need the Lerp in this particular case.)

    You might need to change the constant 0.8 to something smaller or larger (but still between 0 and 1) to get the right camera speed, however. A useful trick is to make this a public variable and change its value while the game is running. Try different values until you get it just right and then make a note of the final value before you stop the running game.
     
  3. Jean-Fabre

    Jean-Fabre

    Joined:
    Sep 6, 2007
    Posts:
    429
    Hi,


    Thanks. It does produce the same unwanted artifact so I assume that my issue is actually not the camera behavior but the actual target that is the cause of this shaking. I will investigate.


    Bye,

    Jean
     
  4. Hanankk

    Hanankk

    Joined:
    Sep 2, 2008
    Posts:
    103

    is your camera shaking too?? cause i have the same effect on the camera and don't know why! in the first i feel that the target that shaking but the target dosen't rotate at all, why is that happened? did u figure out this problem?
    i use the Lerp function also on my camera script.
     
  5. Jean-Fabre

    Jean-Fabre

    Joined:
    Sep 6, 2007
    Posts:
    429
    Hi,

    I did not find a solution no, I just used more damping.
     
  6. Taphos

    Taphos

    Joined:
    Dec 5, 2012
    Posts:
    11