4
votes

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.

1
What doesn't work about it? Are you getting an exception? Is it just not scrolling? - Ageonix
Also, any particular reason you're doing this in LateUpdate instead of Update? - Ageonix
I'm dumb, that worked perfectly. Many thanks. - Tim Hanson

1 Answers

1
votes

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

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