Search Unity

Question Mouse-wheel zoom relative to GO?

Discussion in 'Editor & General Support' started by TangerineDog, Mar 15, 2021.

  1. TangerineDog

    TangerineDog

    Joined:
    Oct 20, 2020
    Posts:
    20
    This is my camera setup:
    Code (CSharp):
    1.         public void Rotate()
    2.         {
    3.             float horizontalRotation = Input.GetAxis("Mouse X");
    4.             float verticalRotation = Input.GetAxis("Mouse Y");
    5.    
    6.             transform.Rotate(0, horizontalRotation * mouseSensitivity, 0);
    7.             cameraHolder.Rotate(verticalRotation * mouseSensitivity, 0, 0);
    8.    
    9.             Vector3 currentRotation = cameraHolder.localEulerAngles;
    10.             if (currentRotation.x > 180) currentRotation.x -= 360;
    11.             currentRotation.x = Mathf.Clamp(currentRotation.x, upLimit, downLimit);
    12.             cameraHolder.localRotation = Quaternion.Euler(currentRotation);
    13.         }
    14.  
    A child GO of my Player has been assigned as the cameraHolder, the main camera has been assigned as that GO's child and offset along the Z axis in order to make the GO the look around pivot point.

    I'd like to zoom to and from that GO with the mouse wheel, essentially only changing the relative position of the camera to the GO along the Z axis.
    Of course, it should apply whichever way the camera is actually facing - so if that translates into some completely other direction in game, the camera should move there.

    How could I achieve that?
     
  2. TangerineDog

    TangerineDog

    Joined:
    Oct 20, 2020
    Posts:
    20