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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Smoothly Interpolate Values Relating To Camera Angle?

Discussion in 'Scripting' started by TheDuples, Apr 14, 2019.

  1. TheDuples

    TheDuples

    Joined:
    Nov 13, 2018
    Posts:
    39
    So effectively I have a camera which is locked onto my player, and the camera has the ability to zoom in on the player using the scroll wheel with set limits for it, however I am trying to figure out how I can make the camera angle change smoothly depending on how far I am from the player.

    Code (CSharp):
    1. void LateUpdate ()
    2.     {
    3.         distance += Input.GetAxis("Mouse ScrollWheel");
    4.         distance = Mathf.Clamp(distance, distanceMinMax.x, distanceMinMax.y);
    5.  
    6.         if (distance < 3)
    7.         {
    8.             // if the camera is closer than 3 units away change the y angle offset of the camera
    9.             offset.y = 2;
    10.         }
    11.         else
    12.         {
    13.             offset.y = 5;
    14.         }
    15.  
    16.         transform.position = target.position + offset * distance;
    17.         transform.LookAt(target);
    18.     }
    This code here works, however the camera's angle changes instantly, but I would like it to change gradually between the two set values. I tried using Lerp, however I'm not sure how to use it in this particular context.
    Please help. Also if anyone has advice on how I can smooth the zoom without changing the sensitivity of the input settings, I would be most gracious.
     
  2. TheDuples

    TheDuples

    Joined:
    Nov 13, 2018
    Posts:
    39
    Please help. I posted this late at night and so many may not have seen it.
     
  3. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,384
    lerp the position and rotation...

    change the last 2 lines to something like:

    Code (csharp):
    1.  
    2. //tweak the 0.5 values to your liking, values closer to 0 are slower, values closer to 1 are fast
    3. transform.position = Vector3.Lerp(transform.position, target.position + offset * distance, 0.5f);
    4. transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(target.position - transform.position), 0.5f);
    5.  
    You can also scale your incrementing of 'distance' as well. Something like:

    Code (csharp):
    1. distance += Input.GetAxis("Mouse ScrollWheel") * speed; //where speed is some configurable value
    Maybe even through Time.deltaTime in there, see what happens...
     
  4. TheDuples

    TheDuples

    Joined:
    Nov 13, 2018
    Posts:
    39
    Both of those suggestions work very nicely, thank you. There is a slight smoothing effect on the camera now, where if my character moves, the camera trails slightly behind, but I don't mind it that much so it's all good. I changed the last line back to transform.LookAt(target), as it doesn't seem to make a difference.