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

Keeping scroll wheel zoom, on a 45 degree angle, centered on player

Discussion in 'Scripting' started by invisibledesign, Apr 15, 2022.

  1. invisibledesign

    invisibledesign

    Joined:
    Jul 25, 2018
    Posts:
    8
    I've got a pretty nice scroll to zoom script on my main camera. It is 90 degree's top down, follows the player and zooms in/out with mouse wheel. It has some clamping for min/max distance and smoothing on the position change.

    What I want to do is change the camera angle to 45 degree's instead of 90. Simply changing the rotation variable to 45 set's the camera angle, but the player is no longer centered on screen. Further to that, the player's position relative to the screen center moves around when zooming in.

    It seems like I need to add the 45 degree rotation into calculating the new position but i'm stuck coming up with anything that works. Anybody got any suggestions?


    Code (CSharp):
    1. public class CameraController : MonoBehaviour
    2. {
    3.  
    4.     private Vector3 velocity = Vector3.zero;
    5.     private GameObject player;
    6.     private float offset = 0.0f;
    7.     private float zoom = 125.0f;
    8.     private float rotation = 90.0f;
    9.     private float smoothTime = 0.3F;
    10.     private float zoomAmount = 0f;
    11.     private float maxToClamp = 10f;
    12.     private float rotSpeed = 10f;
    13.  
    14.     void Start ()
    15.     {
    16.         player = GameObject.FindWithTag(Tags.LOCAL_PLAYER);
    17.  
    18.         //SetOffset();
    19.         SetInitialPosition();
    20.     }
    21.  
    22.     private void LateUpdate()
    23.     {
    24.         ScrollToZoom();
    25.  
    26.         if (player != null)
    27.         {
    28.             //transform.LookAt(player.transform);
    29.             MoveWithDampening();
    30.         }
    31.     }
    32.  
    33.     void SetInitialPosition ()
    34.     {
    35.         transform.position = new Vector3(player.transform.position.x, zoom, player.transform.position.z); // removed - offset
    36.         transform.rotation = Quaternion.Euler(rotation, 0f, 0f);
    37.     }
    38.  
    39.     void MoveWithDampening ()
    40.     {
    41.         Vector3 targetPosition = new Vector3(player.transform.position.x, transform.position.y, player.transform.position.z); // removed - offset
    42.         transform.position = Vector3.SmoothDamp(transform.position, targetPosition, ref velocity, smoothTime);
    43.     }
    44.  
    45.     void ScrollToZoom()
    46.     {
    47.         float input = Input.GetAxis("Mouse ScrollWheel");
    48.         zoomAmount += input;
    49.         zoomAmount = Mathf.Clamp(zoomAmount, -maxToClamp, maxToClamp);
    50.         float translate = Mathf.Min(Mathf.Abs(input), maxToClamp - Mathf.Abs(zoomAmount));
    51.         gameObject.transform.Translate(0, 0, translate * rotSpeed * Mathf.Sign(input));
    52.  
    53.     }
    54.  
    55.     private void SetOffset()
    56.     {
    57.         offset = zoom - rotation;
    58.     }
    59.  
    60. }
     
  2. PanicEnsues

    PanicEnsues

    Joined:
    Jul 17, 2014
    Posts:
    185
    To simplify the problem, I would suggest separating it into pieces: Create a "CamParent' gameObject that follows the player, and make your camera a child of that object.

    Now apply the "follow the player" movement to CamParent, and the zoom* movement to the camera itself.

    If you apply the zoom movement in local space instead of world (use transform.localPosition instead of transform.position), then you can rotate CamParent to any angle you like. The camera will stay pointed at the player and zoom toward/away from them.


    (*Technically it's a "dolly", not a zoom, but that's just cinema semantics. :)
     
  3. invisibledesign

    invisibledesign

    Joined:
    Jul 25, 2018
    Posts:
    8

    Oh thats a great idea thank you. I've never never thought about it like that... I will play around with it. Thanks!