Search Unity

smooth camera script

Discussion in 'Scripting' started by Sync1B, Nov 16, 2005.

  1. Sync1B

    Sync1B

    Joined:
    Sep 13, 2005
    Posts:
    561
    Alright, well I went though the scripting tutorial a lot of things I didn't get, but I'm sure it will come in time. So I started to write a camera smooth script like the one in the tutorial but I wanted mine to be for the Y and X, also using the mouse as an input instead of keyboard. Any way from some reason the it smooths/follows the Y fine but when I add the X part it gets all messed up. I have been messing around with this for hours and thought I needed some help!

    :?

    Code (csharp):
    1.  
    2. //target
    3. var target : Transform;
    4.  
    5. //distance from target
    6. var distance = 10.0;
    7.  
    8. //camera height
    9. var height = 5.0;
    10.  
    11. //damping
    12. var rotationDampingY = 3.0;
    13. var rotationDampingX = 3.0;
    14.  
    15. //how much above the taget we look
    16. var lookHeight = 10.0;
    17.  
    18.  
    19. function LateUpdate () {
    20.     if (!target)
    21.         return;
    22.  
    23.     wantedRotationAngleY = target.eulerAngles.y;
    24.     wantedRotationAngleX = target.eulerAngles.x;
    25.        
    26.     currentRotationAngleY = transform.eulerAngles.y;
    27.     currentRotationAngleX = transform.eulerAngles.x;
    28.    
    29.     //damping
    30.     currentRotationAngleY = Mathf.LerpAngle (currentRotationAngleY, wantedRotationAngleY, rotationDampingY * Time.deltaTime);
    31.     currentRotationAngleX = Mathf.LerpAngle (currentRotationAngleX, wantedRotationAngleX, rotationDampingX * Time.deltaTime);
    32.  
    33.     //degrees to radians
    34.     currentRotationY = Quaternion.EulerAngles (0, currentRotationAngleY * Mathf.Deg2Rad, 0);
    35.     currentRotationX = Quaternion.EulerAngles (currentRotationAngleX * Mathf.Deg2Rad, 0, 0);
    36.    
    37.     //position
    38.     transform.position = target.position - currentRotationY * Vector3.forward * distance + currentRotationX * Vector3.up * height;
    39.    
    40.     //look at target
    41.     transform.LookAt (target.position + Vector3.up*lookHeight);
    42.  
    43.  
    44. }
    45.  
    Thanks
    sync
     
  2. Sync1B

    Sync1B

    Joined:
    Sep 13, 2005
    Posts:
    561
    Maybe some one has a different idea on how to accomplish a camera smooth script?
     
  3. Jonathan Czeck

    Jonathan Czeck

    Joined:
    Mar 17, 2005
    Posts:
    1,713
    What kind of camera style are you going for?

    I'd suggest "smoothing" by using Vector3.Lerp() for the position and Quaternion.Slerp() for the rotation.

    -Jon
     
  4. Sync1B

    Sync1B

    Joined:
    Sep 13, 2005
    Posts:
    561
    It's for a 3rd person game, your in space so I want to be able to look in all directions. I'll give that a try, right after I visit the scripting refrence to figure out how to use them :wink: Ill see what I come up with bbl later with my results.
    Thanks
    sync
     
  5. Sync1B

    Sync1B

    Joined:
    Sep 13, 2005
    Posts:
    561
    Alright I am defiantly more confused.

    1. So .Lerp and .Slerp just find the average 2 numbers higher or lower based on T?

    2. I dont get the difference between them

    3. A example would be extremely helpful

    4. OTEE needs another scripting tutorial some thing to compare to would be nice.

    thanks
    sync[/code]
     
  6. Bampf

    Bampf

    Joined:
    Oct 21, 2005
    Posts:
    369
    Lerp is linear interpolation. This function can be used between two numbers, two vectors/positions, even between two colors.

    Slerp is spherical interpolation (hence the S.) It only applies to rotations. It moves the "average" as you call it along the shortest path on the outside of a sphere. Think of it as being a "nicer but slower" interpolation that only applies to rotations. In general you should use it, not Lerp, for rotations.

    Note also that Lerp/Slerp clamp T to the range [0, 1]. This is very handy when T is a function of time. In effect it means that Lerp won't accidentally move you past the final position/rotation.

    Hope this helps,
    Matt
     
  7. NicholasFrancis

    NicholasFrancis

    Joined:
    Apr 8, 2005
    Posts:
    1,587
    More and more people actually prefer lerping rotations rather than slerping them - in theory, slerping is more correct, as lerping has a tendency to speed up in the middle.

    This speedup makes it feel more solid as you get an acceleration/decelleration for free. Any errors Lepr would give you usually are lost because you do the rotation adjustments over many frames.

    They're fairly easy to switch, so try both and if you can't tell the difference, just use Lerp.