Search code examples
c#inputunity-game-enginescrollwheel

Mouse scroll wheel input not being recognised in unity


Trying to get a zooming camera script to work and it's not. My other parts of the script function fine, except this one and I think it's something to do with mouse scroll wheel.

void LateUpdate()
{
    if (!EventSystem.current.IsPointerOverGameObject())
    {     
        if(Input.GetAxis("Mouse ScrollWheel")<0)
        {
            CameraZoom();
        }
    }
}

public void CameraZoom()
{
    if (!EventSystem.current.IsPointerOverGameObject())
    {
        distance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel") * zoomFactor, distanceMin, distanceMax);
        RaycastHit hit;

        if (Physics.Linecast(target.position, transform.position, out hit))
        {
            distance -= hit.distance;
        }
    }
}

I just want it to zoom when I move the mouse wheel, but I need it to be a public void so that I can access it from other scripts, mainly easy touch.


Solution

  • Try putting this code in Update() instead of LateUpdate().

    void Update()
    {
    if (!EventSystem.current.IsPointerOverGameObject())
        {     
        if(Input.GetAxis("Mouse ScrollWheel")<0)
            {
                CameraZoom();
            }
        }
    }