Search Unity

New Input System, Mouse Scroll Wheel and Zoom

Discussion in 'Input System' started by Deleted User, Sep 11, 2020.

  1. Deleted User

    Deleted User

    Guest

    Hiya guys,

    Just playing around with the new input system and trying to get the mouse scroll to work with the Camera.main.fieldOfView to zoom in.

    However, all the code I write comes up with an "invalid operation exception vector2control" error. Not sure why.

    Code (CSharp):
    1.  private void Awake()
    2.     {
    3.        // cameraOffset = transform.position - playerTF.position;
    4.  
    5.         controls = new Controls();
    6.         controls.Move.MoveScroll.performed += ctx => Scroll();
    7.  
    8.     }
    9.  
    10.  
    11.     private void Scroll()
    12.     {  
    13.         Camera.main.fieldOfView += controls.Move.MoveScroll.ReadValue<Vector2>().y;
    14.     }
     
  2. Bezoro

    Bezoro

    Joined:
    Mar 16, 2015
    Posts:
    133
    I do believe the actual value you want to read is in the ctx variable, pass it into Scroll() then use it instead of controls.
     
  3. stereoplanet

    stereoplanet

    Joined:
    Apr 14, 2014
    Posts:
    2
    Hey! I'm trying to figure out the same thing - do you have an example of how to make this work? The concept of the ctx isn't really clicking with me yet....
     
  4. kasuyakema

    kasuyakema

    Joined:
    Sep 13, 2020
    Posts:
    11
    You can register a Callback that takes in the context as a parameter. Then you can read the value from the context:

    Code (CSharp):
    1.  
    2.     controls.Player.Look.performed += ctx => OnLook(ctx);
    3.  
    4.     public void OnLook(InputAction.CallbackContext ctx)
    5.     {
    6.         Vector2 v = (Vector2)ctx.ReadValueAsObject();
    7.         characterController.Look(v);
    8.     }
    9.