Search Unity

Simple Smooth Slew help!

Discussion in 'Scripting' started by Carwash, Nov 16, 2009.

  1. Carwash

    Carwash

    Joined:
    Nov 12, 2009
    Posts:
    95
    Hello,

    Not a coder, trying to get my head round this. I'm trying to make a following camera have some lag behind the movements of the target it's following along all three axis, but not rotations.

    Here's what i've tried to cobble together from the standard Smooth Follow:


    Code (csharp):
    1. // The target we are following
    2. var target : Transform;
    3.  
    4. // How much we
    5. var heightDamping =1;
    6. var SlewDamping = 1;
    7. var ForAftDamping = 1;
    8.  
    9.  
    10. function LateUpdate () {
    11.     // Early out if we don't have a target
    12.     if (!target)
    13.         return;
    14.    
    15.     // Calculate the current planes
    16.     wantedSlew = target.position.x ;
    17.     wantedHeight = target.position.y ;
    18.     wantedForAft = target.position.z ; 
    19.    
    20.     currentSlew = transform.position.x ;
    21.     currentHeight = transform.position.y ;
    22.     currentForAft = transform.position.z ;
    23.  
    24.  
    25.     // Damp the slew
    26.     CurrentSlew = Mathf.Lerp (currentSlew, wantedSlew, SlewDamping * Time.deltaTime);
    27.  
    28.     // Damp the height
    29.     CurrentHeight = Mathf.Lerp (currentHeight, wantedHeight, heightDamping * Time.deltaTime);
    30.  
    31.     // Damp the ForAft
    32.     CurrentForAft = Mathf.Lerp (currentForAft, wantedForAft, ForAftDamping * Time.deltaTime );
    33.  
    34.  
    35.     // Always look at the target
    36.     transform.LookAt (target);
    37. }

    I'm sure it's dead obvious what's wrong to you guys but I'm flummoxed. Anyone care to educate me?
     
  2. DMJ

    DMJ

    Joined:
    Nov 5, 2009
    Posts:
    83
    It helps to tell us what it does that is wrong, so we might have an idea of what part of the code to concentrate on. What is it doing that you don't want it to do?
     
  3. Carwash

    Carwash

    Joined:
    Nov 12, 2009
    Posts:
    95
    Oh sorry! The camera is just following the target normally, i.e there's no lag between target movement and camera movement. I'm trying to make the camera's movement lag slightly behind the target (preferably as a variable i can quickly adjust for testing).

    Thanks for letting me know what was wrong with my question!
     
  4. DMJ

    DMJ

    Joined:
    Nov 5, 2009
    Posts:
    83
    Sorry, I'm not the best person to ask right now, I'm suffering from a lack of sleep (too much Unity scripting until the late hours of the morning, to be honest!).

    Have you tried tweaking the damping amounts? Depending on how fast the target object is moving, you might not notice a Lerp with a "damping" value of 1.0. Try making it 0.1, which is a sloooooow lerp.

    When I'm lerping positions I tend to store the positions as two Vector3 variables, since they come with their own handy-dandy Vector3.Lerp() function, although you don't get the lerp-amount control over each axis as your method does.
     
  5. duck

    duck

    Unity Technologies

    Joined:
    Oct 21, 2008
    Posts:
    358
    Seems to me there's a couple of things wrong with this.

    First off, it seems you're declaring 6 variables, 3 of whose names differ only by case (the capitalisation of the first letter):

    currentSlew, currentHeight, currentForAft
    CurrentSlew, CurrentHeight, CurrentForAft

    In Unity (yes even in Unity's JS), variables are case sensitive, so the second set of 3 variables are different and separate from the first 3, so this may cause you some confusion if it wasn't intentional in your script.

    Secondly, there seems to be nowhere in your code where these values are actually applied to the camera's position - so I don't understand how you are seeing your camera move at all, unless by 'move' you mean rotate!

    And lastly - and these are aesthetic rather than function problems - if you're going for proper terminology, it's "Fore" not "For". Plus, "Slew" is generally used to describe the rotation around the Z axis, so it's not really appropriate for a variable which represents a positional value.

    hope this helps!

    - Ben
     
  6. Carwash

    Carwash

    Joined:
    Nov 12, 2009
    Posts:
    95
    Thanks guys.