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. Dismiss Notice

Using Input.GetAxis("Mouse ScrollWheel") to move the main camera

Discussion in 'Scripting' started by moynzy, Nov 4, 2014.

  1. moynzy

    moynzy

    Joined:
    Oct 22, 2014
    Posts:
    82
    Hey guys, I'm trying to implement a zoom camera feature where if the player scrolls the mouse wheel they will be able to change the camera perspective from first person to third person.

    This is popular mmo games, such as WOW, FF13, ect.


    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class ZoomInOut : MonoBehaviour
    6. {
    7.     public Camera camera;
    8.     public float xOffSet;
    9.     public float yOffset;
    10.     public float zOffSet;
    11.  
    12.     public float po = 1.3f;
    13.  
    14.  
    15.     void  Start ()
    16.     {
    17.         yOffset = 7.2f;
    18.         zOffSet =1.4f;
    19.         xOffSet = -1.4f;
    20.         Camera.main.transform.Translate(xOffSet,yOffset,zOffSet);
    21.  
    22.     }
    23.     void  Update ()
    24.     {
    25.         //yOffset = Input.GetMouseButton("
    26. //        if(Input.GetAxis("Mouse ScrollWheel")){
    27. //                    yOffset++;
    28. //        }
    29.         //Camera.main.transform.Translate(xOffSet,yOffset,zOffSet);
    30.  
    31.         if(Input.GetAxis("Mouse ScrollWheel")){
    32.             xOffSet = Input.GetAxis("Mouse ScrollWheel");
    33.             yOffset = Input.GetAxis("Mouse ScrollWheel");
    34.             zOffSet = Input.GetAxis("Mouse ScrollWheel");
    35.             Camera.main.transform.Translate(xOffSet,yOffset,zOffSet);
    36.  
    37.         }
    38.  
    39.     }
    40. }
    41. }
    In the start function i initialize where I want my main camera to be and then I pass these variables in to the arguments.

    In the update function, i only want to update the variables when the mouse is being scrolled. However, i get an error saying
    From Google - ing this error I found out that :
    So I don't see the problem in the code, unless I have implemented it wrong

    Code (CSharp):
    1.     void  Update ()
    2.     {
    3.         //yOffset = Input.GetMouseButton("
    4. //        if(Input.GetAxis("Mouse ScrollWheel")){
    5. //                    yOffset++;
    6. //        }
    7.         //Camera.main.transform.Translate(xOffSet,yOffset,zOffSet);
    8.  
    9.         //if(Input.GetAxis("Mouse ScrollWheel")){
    10.             xOffSet = Input.GetAxis("Mouse ScrollWheel");
    11.             yOffset = Input.GetAxis("Mouse ScrollWheel");
    12.             zOffSet = Input.GetAxis("Mouse ScrollWheel");
    13.             Camera.main.transform.Translate(xOffSet,yOffset,zOffSet);
    14.  
    15.         //}
    16.  
    17.     }
    when I ran this, the x,y, and z variables where increasing despite the scroll wheel being touched.

    My question is, how can I get the same affect that most games implement with the scroll feature.

    of course there will be boundaries such as.

    if xOffSet > minXOffSet then xOffSet = minXOffSet.

    I've tried to google this, but they had use the zoom affect, where it stretches the map.
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,377
    You're moving the camera in all 3 directions by the delta of the mouse wheel.

    What I'd do is have a vector representing the 'near' position. And another representing the 'far' position. Then I'd have a float representing a percentage (0->1) you're at between near and far. The scroll wheel delta will adjust this value back and forth (note you'll have to divide the delta by some larger value... this value will act as a speed, small values being faster).

    Then you just set the position to the vector lerped from near to far by this percentage value.
     
  3. moynzy

    moynzy

    Joined:
    Oct 22, 2014
    Posts:
    82
    Hey, i kinda got half of that, since i'm not that illiterate.

    Can you break that down into pseudo please?

    Thank you.
     
  4. GreenJoy

    GreenJoy

    Joined:
    Jul 1, 2016
    Posts:
    2
    Hey,man!
    Look at the API, the Input.GetAxis("Mouse ScrollWheel"); script return a float value,when the Mouse stay,it return 0,for forward scrolling returning positive value,and backward scrolling return negative value. So maybe you should write in Update function like:
    if (Input.GetAxis ("Mouse ScrollWheel") != 0) {
    // dosomething...
    }