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. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

Add Camera Damping at Rotation and Position ..

Discussion in 'Scripting' started by Terraya, Mar 15, 2020.

  1. Terraya

    Terraya

    Joined:
    Mar 8, 2018
    Posts:
    602
    Hey Guys,

    since im working on several things,
    i get back to my Camera Script,

    actualy it works super good but i also would like to add kinda Damping effect but i cant realy figure out how,

    maybe someone can help me quickly out,

    cheers! :)

    Code (CSharp):
    1.     void CheckForRotation()
    2.     {
    3.         //GETS CONTROLLER INPUT
    4.         float inputX = Input.GetAxis("RightStickHorizontal");
    5.         float inputZ = Input.GetAxis("RightStickVertical");
    6.  
    7.         //MOUSE INPUT
    8.         mouseX = Input.GetAxis("Mouse X");
    9.         mouseY = Input.GetAxis("Mouse Y");
    10.  
    11.         //CONTROLLER + MOUSE INPUT (IN CASE SOMEONE USES CONTROLLER)
    12.         finalInputX = inputX + mouseX;
    13.         finalInputZ = inputZ + mouseY;
    14.  
    15.         //GETS THE Y AND X ROTATION TIMES THE SENSIVITY AND THE TIME(so it look good)
    16.         rotY += finalInputX * inputSensitivity * Time.deltaTime;
    17.         rotX += finalInputZ * inputSensitivity * Time.deltaTime;
    18.  
    19.         //RETURNS X (Clamping Works also while turning)
    20.         rotX = Mathf.Clamp(rotX, -clampAngle, clampAngle);
    21.  
    22.         rotY = ClampAngle(rotY, -(CameraFollowObj.transform.localEulerAngles.y + clampAngle), CameraFollowObj.transform.localEulerAngles.y + clampAngle);
    23.  
    24.         Quaternion localRotation = Quaternion.Euler(rotX, rotY + PlayerObj.transform.eulerAngles.y, 0.0f);
    25.         transform.localRotation = localRotation;
    26.     }
    27.  
    28.     void CameraUpdater()
    29.     {
    30.         // set the target object to follow
    31.         Transform target = CameraFollowObj.transform;
    32.         //move towards the game object that is the target
    33.         float step = cameraMoveSpeed * Time.deltaTime;
    34.         transform.position = Vector3.MoveTowards(transform.position, target.position, step);
    35.     }
    36.  
    37.     public float ClampAngle(float angle, float min, float max)
    38.     {
    39.         if (angle < -360F) { angle += 360F; }
    40.         if (angle > 360F) { angle -= 360F; }
    41.         return Mathf.Clamp(angle, min, max);
    42.     }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    29,558
    One way is to use Cinemachine. :)

    Another way using the scripts above is to change cameraMoveSpeed gradually downwards as you approach your target.

    So you would leave cameraMoveSpeed alone and make a new local variable in CameraUpdater();

    That variable would be initially cameraMoveSpeed.

    Then you would compute the distance from
    target.position
    to
    transform.position
    and when it gets below a certain distance, you would begin to slow cameraMoveSpeed down to a miminum version of it.

    For instance (I am just typing this, not compiling it):

    Code (csharp):
    1. // this is the presumed speed we will use
    2. float tempSpeed = cameraMoveSpeed;
    3.  
    4. // until we get close
    5. float distance = Vector3.Distance (target.position, transform.position);
    6.  
    7. float close = 2.0f;  // change as needed
    8.  
    9. if (distance < close)
    10. {
    11.   // now we need to adjust tempSpeed from its max down to its minimum slowly
    12.  
    13.   float slowestCameraSpeed = 0.5f;   // we'll slow to this value
    14.  
    15.   float alpha = distance / close;   // alpha will go from 1.0f to 0.0f
    16.  
    17.   // now we lerp to the slower speed as alpha goes from 1.0 to 0.0
    18.   tempSpeed = Mathf.Lerp( slowestCameraSpeed, tempSpeed, alpha);
    19. }
    20.  
    21. tempSpeed = tempSpeed * Time.deltaTime;
    22.  
    23. // now you can use tempSpeed in place of step with your original Vector3.MoveTowards() function.
    24.  
     
    Terraya likes this.
  3. Terraya

    Terraya

    Joined:
    Mar 8, 2018
    Posts:
    602
    Thanks, i will try it out tomorrow for sure!

    But isnt there another simple way from Unity like Vector3.SmoothDamp and so on?

    have been trying to figure something out with them but not realy working out good
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    29,558
    If you want simple, just use Cinemachine.

    I like to understand what I'm doing because it helps my brain form new connections and understand more things in Unity and gamedev in general, so I'm happy to develop some step-by-step operations that I can understand today, as well as two years from now if I dig this code up again.
     
    Terraya likes this.
  5. Terraya

    Terraya

    Joined:
    Mar 8, 2018
    Posts:
    602
    Thats Perfect,

    well i will try later on your suggestion, looks simple and the idea behind it is actualy nice! :)
     
  6. Terraya

    Terraya

    Joined:
    Mar 8, 2018
    Posts:
    602
    So for now my "CameraFollowing" does work with damping,

    i did it a bit simplier as i found in the Unity.Doc a simple function,

    Code (CSharp):
    1.     void CameraUpdater()
    2.     {
    3.         // set the target object to follow
    4.         Transform target = CameraFollowObj.transform;
    5.         //move towards the game object that is the target
    6.         float step = cameraMoveSpeed * Time.deltaTime;
    7.         //If Damping is "on", the Camera will Damp depending on the Smooth value
    8.         if(useCameraFollowDamping){
    9.             transform.position = Vector3.SmoothDamp(transform.position, target.position, ref velocity, followDamping);
    10.             return;
    11.         }
    12.         transform.position = Vector3.MoveTowards(transform.position, target.position, step);
    13.     }
    so now i still need kind of "Damping" for my "Rotation", if i get something, i will Update it here
     
  7. Raxwe

    Raxwe

    Joined:
    Oct 23, 2020
    Posts:
    80
    cinemachine is the best
     
  8. Terraya

    Terraya

    Joined:
    Mar 8, 2018
    Posts:
    602
    Well, its an Old Thread, i did fix it thanks to @Kurt-Dekker ,
    step by step, would suggest it to anyone else :)

    i end up with Cinemachine after coding it myself since Cinemachine offers many more features out of the box :)
     
    Kurt-Dekker likes this.
  9. radiantboy

    radiantboy

    Joined:
    Nov 21, 2012
    Posts:
    1,528
    what cinema machine component did you use?
    and does it work if camera is inside something like head?
     
unityunity