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

Move and zoom target camera at the same time

Discussion in 'Scripting' started by quarksrule, Oct 10, 2019.

  1. quarksrule

    quarksrule

    Joined:
    Apr 28, 2019
    Posts:
    4
    Hi guys,
    I found this awesome script for a target camera, but I want to add an iTween animation to it, where if a user presses a keyboard key, the camera moves to a new target position and zooms in at the same time. I figured out how to get it to move, but I can't get it to zoom in while its moving..

    Here is the script


    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class camera : MonoBehaviour
    4. {
    5.  
    6.      public Transform target;
    7.      public Vector3 targetOffset;
    8. public Transform moveTarget;
    9.  
    10.      public float distance = 5.0f;
    11.      public float maxDistance = 20;
    12.      public float minDistance = .6f;
    13.      public float xSpeed = 200.0f;
    14.      public float ySpeed = 200.0f;
    15.      public int yMinLimit = -80;
    16.      public int yMaxLimit = 80;
    17.      public int zoomRate = 40;
    18.      public float panSpeed = 0.3f;
    19.      public float zoomDampening = 5.0f;
    20.      private float xDeg = 0.0f;
    21.      private float yDeg = 0.0f;
    22.      private float currentDistance;
    23.  
    24.      float distanceBetween;
    25.      private float desiredDistance;
    26.      private Quaternion currentRotation;
    27.      private Quaternion desiredRotation;
    28.      private Quaternion rotation;
    29.      private Vector3 position;
    30.      void Start() { Init(); }
    31.      void OnEnable() { Init(); }
    32.      public void Init()
    33.      {
    34.          //If there is no target, create a temporary target at 'distance' from the cameras current viewpoint
    35.          if (!target)
    36.          {
    37.              GameObject go = new GameObject("Cam Target");
    38.              go.transform.position = transform.position + (transform.forward * distance);
    39.        
    40.          }
    41.          distance = Vector3.Distance(transform.position, target.position);
    42.          currentDistance = distance;
    43.          desiredDistance = distance;
    44.          //be sure to grab the current rotations as starting points.
    45.          position = transform.position;
    46.          rotation = transform.rotation;
    47.          currentRotation = transform.rotation;
    48.          desiredRotation = transform.rotation;
    49.          xDeg = Vector3.Angle(Vector3.right, transform.right);
    50.          yDeg = Vector3.Angle(Vector3.up, transform.up);
    51.      }
    52.      /*
    53.       * Camera logic on LateUpdate to only update after all character movement logic has been handled.
    54.       */
    55.      void LateUpdate()
    56.      {
    57.  
    58.    
    59.         //Camera Orbit
    60.  
    61.          if (Input.GetMouseButton(0))
    62.          {
    63.              xDeg += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
    64.              yDeg -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
    65.    
    66.              //Clamp the vertical axis for the orbit
    67.              yDeg = ClampAngle(yDeg, yMinLimit, yMaxLimit);
    68.    
    69.              desiredRotation = Quaternion.Euler(yDeg, xDeg, 0);
    70.              currentRotation = transform.rotation;
    71.              rotation = Quaternion.Lerp(currentRotation, desiredRotation, 2f);
    72.              transform.rotation = rotation;
    73.          }
    74.  
    75.  
    76. //NEED SOLUTION HERE!
    77.          // Press B to move camera target AND zoom in on the target - how ?
    78.  
    79.              if (Input.GetKeyUp(KeyCode.B)) {
    80.               iTween.MoveTo(target.gameObject,moveTarget.position,0.8f);        
    81.  
    82.             }
    83.  
    84.  
    85.    
    86.          // affect the desired Zoom distance if we roll the scrollwheel
    87.  
    88.          desiredDistance -= Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime * zoomRate * Mathf.Abs(desiredDistance);
    89.          //clamp the zoom min/max
    90.          desiredDistance = Mathf.Clamp(desiredDistance, minDistance, maxDistance);
    91.          // For smoothing of the zoom, lerp distance
    92.          currentDistance = Mathf.Lerp(currentDistance, desiredDistance, Time.deltaTime * zoomDampening);
    93.          // calculate position based on the new currentDistance
    94.          position = target.position - (rotation * Vector3.forward * currentDistance + targetOffset);
    95.          transform.position = position;
    96.  
    97.    
    98.      }
    99.      private static float ClampAngle(float angle, float min, float max)
    100.      {
    101.          if (angle < -360)
    102.              angle += 360;
    103.          if (angle > 360)
    104.              angle -= 360;
    105.          return Mathf.Clamp(angle, min, max);
    106.      }
    107. }
    Will appreciate all the help I can get, thanks.
     
    Last edited: Oct 11, 2019
  2. TheHammer1

    TheHammer1

    Joined:
    Oct 15, 2015
    Posts:
    25
    Go to your camera component. You know that "Field of View" property? Mess with the Field of View slider a little bit: this changes how much the camera is zoomed in. You can use a coroutine to zoom. You'll also need your variables to play a role in this process (such as zoomRate). Alternatively, you could manipulate the camera so that it actually moves towards to object to zoom in. I prefer using the FoV property.

    For having movement and zooming at the same time, you could use async to run two methods at the same time: see this.