Search Unity

Smooth mouse scroll camera zoom help?

Discussion in 'Scripting' started by cyberianhd, Aug 31, 2012.

  1. cyberianhd

    cyberianhd

    Joined:
    Aug 20, 2012
    Posts:
    51
    I need help with a script that i need help on, i need it so when i scroll it zooms in very smoothly instead of the script now when you zoom in it is choppy looking and bad. Thanks! Here is my script:
    Code (csharp):
    1. function Update ()
    2. {
    3. //------------------Code for Zooming Out------------
    4.    if (Input.GetAxis("Mouse ScrollWheel") <0)
    5.        {
    6.    if (Camera.main.fieldOfView<=50)
    7.    Camera.main.fieldOfView +=2;
    8.    if (Camera.main.orthographicSize<=20)
    9.                                    Camera.main.orthographicSize +=0.5;
    10.        }
    11.  
    12. //----------------Code for Zooming In-----------------------
    13.     if (Input.GetAxis("Mouse ScrollWheel") > 0)
    14.        {
    15.     if (Camera.main.fieldOfView>20)
    16.     Camera.main.fieldOfView -=2;
    17.     if (Camera.main.orthographicSize>=1)
    18.                                 Camera.main.orthographicSize -=0.5;
    19.        }
    20.        }
     
  2. blackstar

    blackstar

    Joined:
    Aug 29, 2012
    Posts:
    19
    add a motion blur effect with ammount 0.2 to main camera and it will be good..
     
  3. cyberianhd

    cyberianhd

    Joined:
    Aug 20, 2012
    Posts:
    51
    this didnt help at all, please can someone help with a better answer?
     
  4. riifk1215

    riifk1215

    Joined:
    Jul 24, 2012
    Posts:
    9
    You can use the Mathf.Lerp(), like the code:
    Code (csharp):
    1. var distance : float = 60;
    2.     var sensitivityDistance : float = 50;
    3.     var damping : float = 5;
    4.    
    5.     var minFOV : float = 40;
    6.     var maxFOV : float = 60;
    7.    
    8.     function Start ()
    9.     {
    10.         distance = camera.fieldOfView;
    11.     }
    12.    
    13.     function Update ()
    14.     {
    15.         distance -= Input.GetAxis("Mouse ScrollWheel") * sensitivityDistance;
    16.         distance = Mathf.Clamp(distance, minFOV, maxFOV);
    17.         camera.fieldOfView = Mathf.Lerp(camera.fieldOfView, distance, Time.deltaTime * damping);
    18.     }
     
  5. kingcharizard

    kingcharizard

    Joined:
    Jun 30, 2011
    Posts:
    1,137
    I think its choppy because u increase the FOV by +=2 try playing with the value

    If that still doesn't help try using LateUpdate()

    Here is an alternative way to make a camera zoom in and out with the mouse wheel

    Code (csharp):
    1.  
    2.         //Vector3 zoom variable.
    3.         Vector3 zoom = transform.position;
    4.  
    5.         if(zoom.y < minZoom){
    6.             zoom.y = minZoom;
    7.             transform.position = zoom;
    8.         }
    9.         if(zoom.y > maxZoom){
    10.             zoom.y = maxZoom;
    11.             transform.position = zoom;
    12.         }
    13.    if(Input.GetAxis("Mouse ScrollWheel")< 0 || Input.GetKey(KeyCode.Alpha2)){
    14.             if(zoom.y < maxZoom){
    15.             transform.Translate(Vector3.back * Time.deltaTime * scrollSpeed);
    16.             }
    17.         }
    18.  
    19.         if(Input.GetAxis("Mouse ScrollWheel") > 0 || Input.GetKey(KeyCode.Alpha1)){
    20.             if(zoom.y > minZoom){
    21.             transform.Translate(Vector3.forward * Time.deltaTime * scrollSpeed);
    22.             }
    23.         }
    24.  
    I wrote this to work with an RTS style camera so I dunno if it will help at all just an idea. This moved the actual camera that was located at a fixed z position(If i remember correctly).

    I personally rather move the camera than adjust the FieldOfView but you might not want to move the camera..
     
    Last edited: Sep 5, 2012