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

MacBook Trackpad with Mouse Delta and Scroll/Y

Discussion in 'Input System' started by andycodes, Nov 30, 2021.

  1. andycodes

    andycodes

    Joined:
    Jan 22, 2013
    Posts:
    39
    I've been testing out the Input System with a mouse, Xbox controller, and the Trackpad on my laptop. For the simple demo, I track Mouse Delta to look around the player in a 3rd person view (right stick on controller), and I also have a camera zoom which uses the scroll wheel (left / right triggers on controller).

    On the controller and mouse, these work just fine and as expected, however on my Macbook Trackpad, I'm noticing that when using two fingers to gesture a mouse scroll, I seem to be getting varying amounts of x-axis delta from the mouse input. This has become somewhat bothersome because the camera will start rotating around the player while I'm trying to zoom in and out. The frustrating part with this, though, is that it isn't a consistent amount; sometimes it isn't present, sometimes its enough to rotate fully around the player from the trip between min and max zoom.

    I've attached the camera script below if there's anything I'm doing incorrectly that would be cause for it to have this strange behavior.

    Code (CSharp):
    1. public class CameraFollow : MonoBehaviour
    2. {
    3.     PlayerControls controls;
    4.  
    5.     public Transform target;
    6.  
    7.     [Range(0, 0.5f)]
    8.     public float scrollSpeed = 0.1f;
    9.     public float speed = 5.0f;
    10.     public float followDistance = 10.0f;
    11.  
    12.     [Header("Rotation Limits")]
    13.     [Range(-90, 90)]
    14.     public float yLookMin = -20.0f;
    15.     [Range(-90, 90)]
    16.     public float yLookMax = 85.0f;
    17.  
    18.     [Header("Distance Limits")]
    19.     [Range(1, 50)]
    20.     public float minFollowDistance = 2.0f;
    21.     [Range(1, 50)]
    22.     public float maxFollowDistance = 20.0f;
    23.  
    24.     private float xAxis = 0.0f;
    25.     private float yAxis = 0.0f;
    26.     private Vector2 lookDelta;
    27.  
    28.     void Awake()
    29.     {
    30.         controls = new PlayerControls();
    31.  
    32.         controls.Gameplay.Look.performed += ctx => lookDelta = ctx.ReadValue<Vector2>();
    33.         controls.Gameplay.Look.canceled += ctx => lookDelta = Vector2.zero;
    34.  
    35.         controls.Gameplay.Zoom.performed += ctx => Zoom(ctx.ReadValue<float>());
    36.     }
    37.  
    38.     void Start()
    39.     {
    40.         Assert.IsNotNull(target);
    41.     }
    42.  
    43.     void Update()
    44.     {
    45.         float yLook = yAxis - lookDelta.y * speed;
    46.         yAxis = Mathf.Clamp(yLook, yLookMin, yLookMax);
    47.         xAxis += lookDelta.x * speed;
    48.         Quaternion rotation = Quaternion.Euler(yAxis, xAxis, 0.0f);
    49.         transform.rotation = rotation;
    50.         transform.position = target.position + rotation * new Vector3(0.0f, 0.0f, -followDistance);
    51.     }
    52.  
    53.     private void OnEnable()
    54.     {
    55.         controls.Enable();
    56.     }
    57.  
    58.     private void OnDisable()
    59.     {
    60.         controls.Disable();
    61.     }
    62.  
    63.     private void Zoom(float delta)
    64.     {
    65.         followDistance += delta * scrollSpeed;
    66.         followDistance = Mathf.Clamp(followDistance, minFollowDistance, maxFollowDistance);
    67.     }
    68. }
    69.  
     
  2. andycodes

    andycodes

    Joined:
    Jan 22, 2013
    Posts:
    39
    As supplemental evidence, Using this same approach with the old Input Manager does not produce this x-axis drift:

    Code (CSharp):
    1.     void Update()
    2.     {
    3.         float yLook = yAxis - Input.GetAxis("Mouse Y") * speed;
    4.         yAxis = Mathf.Clamp(yLook, yLookMin, yLookMax);
    5.         xAxis += Input.GetAxis("Mouse X") * speed;
    6.  
    7.  
    8.         followDistance += Input.GetAxis("Mouse ScrollWheel") * scrollSpeed;
    9.         followDistance = Mathf.Clamp(followDistance, minFollowDistance, maxFollowDistance);
    10.  
    11.         Quaternion rotation = Quaternion.Euler(yAxis, xAxis, 0.0f);
    12.  
    13.         transform.rotation = rotation;
    14.  
    15.         transform.position = target.position + rotation * new Vector3(0.0f, 0.0f, -followDistance);
    16.  
    17.     }
     
  3. jardineiro

    jardineiro

    Joined:
    Jan 13, 2022
    Posts:
    2
    This is so annoying. Also when I click the mouse delta value is affected. Unusable!