Reputation: 228
I have an image that I have setup to move around and zoom in and out from. The trouble is the zoom can be done from anywhere in the scene, but I only want it to zoom when the mouse is hovering over the image. I have tried to use OnMouseEnter, OnMouseOver, event triggers, all three of those without a collider, with a collider, with a trigger collider, and all of that on the image itself and on an empty game object. However none of those have worked...So I am absolutely stumped...Could someone help me out here!
Here is my script:
private float zoom;
public float zoomSpeed;
public Image map;
public float zoomMin;
public float zoomMax;
void Update () {
zoom = (Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime * zoomSpeed);
map.transform.localScale += new Vector3(map.transform.localScale.x * zoom, map.transform.localScale.y * zoom, 0);
Vector3 scale = map.transform.localScale;
scale = new Vector3(Mathf.Clamp(map.transform.localScale.x, zoomMin, zoomMax), Mathf.Clamp(map.transform.localScale.y, zoomMin, zoomMax), 0);
map.transform.localScale = scale;
}
Upvotes: 3
Views: 6615
Reputation: 41
This can be solved pretty easily and cleanly. You can add an event trigger onto your image object. Inside the event trigger component add two event types: Pointer Enter, and Pointer Exit. From that point on I would go into the scrolling script and add a bool called canScroll, and two public methods that are called something like OnHover and OnHoverExit. When OnHover is true, canScroll is true and vice versa. Then you can hook up the functions in the Event Trigger.
Upvotes: 4