- Home /
Mouse Wheel Zoom
Hey all, I have a ortho camera, I can't figure out how to make it so the mouse wheel will scroll in or out. I need the script to change main.camera.orthographicSize in increments of 1. Max 6 Min 1.
If you provide a script can you give me a brief explanation of how it works, specifically how I code for zoom in vs zoom out?
Thanks! Joe
Answer by AnaRhisT · Jun 22, 2010 at 08:58 AM
function Update () { if (Input.GetAxis("Mouse ScrollWheel") < 0) // back { Camera.main.orthographicSize = Mathf.Max(Camera.main.orthographicSize-1, 1);
}
if (Input.GetAxis("Mouse ScrollWheel") > 0) // forward
{
Camera.main.orthographicSize = Mathf.Min(Camera.main.orthographicSize-1, 6);
}
}
Not exactly what I wanted, but close enough! I added if < 5 then increment, if > 1 decrement and changed it to go 1 at a time ins$$anonymous$$d of random. I just wasn't sure how to use the mouse scrollwheel deal, but it makes more sense now!
Edited the code to make more sense to anyone reading it later
simply excellent! This even worked in C#! Thanks.
-Harry
I have problems with the & g t ; it gives me complier error and i dont really know what its good for?
Answer by _Petroz · Jun 22, 2010 at 11:20 AM
function Update () { const int orthographicSizeMin = 1; const int orthographicSizeMax = 6;
if (Input.GetAxis("Mouse ScrollWheel") > 0) // forward
{
Camera.main.orthographicSize++;
}
if (Input.GetAxis("Mouse ScrollWheel") < 0) // back
{
Camera.main.orthographicSize--;
}
Camera.main.orthographicSize = Mathf.Clamp(Camera.main.orthographicSize, orthographicSizeMin, orthographicSizeMax );
}
Talking of which, theres some weird bugs using consts inside functions - you may want to watch out for using them for anything maths based
Thank you for this tip, I was working on a RTS Camera Controller and this helped. I found a way to smoothly interpolate the camera's zoom.
Answer by Awanonimik · Aug 10, 2017 at 01:05 AM
Comprehensive code, fully usable without editing:
public class ExampleClass : MonoBehaviour {
public float scrollDistanceIncrement;
// Optional |: public float maxScroll;
void Update () {
// Get mouse scrollwheel forwards || check if main camera is too small, prevents a couple errors
if (Input.GetAxis ("Mouse ScrollWheel") > 0 && Camera.main.orthographicSize > 1) {
// Scroll camera inwards
Camera.main.orthographicSize = Camera.main.orthographicSize -= scrollDistanceIncrement;//Mathf.Max (Camera.main.orthographicSize - 1, 1);
}
// Get mouse scrollwheel backwards || optional code
if (Input.GetAxis ("Mouse ScrollWheel") < 0 /*Optional |: && Camera.main.orthographicSize < maxScroll :| */) {
// Scrolling Backwards
Camera.main.orthographicSize = Camera.main.orthographicSize += scrollDistanceIncrement;//Mathf.Min (Camera.main.orthographicSize - 1, 1);
}
}
/* Legend:
* |: <code> :| is code
* || are just seperators
*/
}
Upvote if this was helpful.
Answer by Semyonys · May 31, 2015 at 12:27 PM
i try to do some code like this:
if (Input.GetAxis("Mouse ScrollWheel") > 0)
{
camera.orthographicSize--;
}
if (Input.GetAxis("Mouse ScrollWheel") < 0)
{
camera.orthographicSize++;
}
But unity act on this only when i am holding CTRL.
Answer by sotirosn · Nov 03, 2013 at 09:50 PM
you can use this expression to get back -1, 0, or 1.
(Input.GetAxis("MouseWheel") != 0 ? Mathf.Sign(Input.GetAxis("MouseWheel")) : 0)
and then just do it all with one line like,
Camera.main.orthographicSize = Mathf.Clamp(Camera.main.orthographicSize + (Input.GetAxis("MouseWheel) != 0 ? Mathf.Sign(Input.GetAxis("MouseWheel")) : 0), zoomMin, zoomMax);
Actually I am having a problem where I only want to use mouse input if the user holds down the shift key. But it seems the mouse wheel does create input if shift is held down. Does anyone else have this problem?
@sotirosn, I'm having the same issue w Shift + Scroll not working...did you find a solution?
Couldn't you just do
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.LeftShift))
Do$$anonymous$$ouseWheelStuff();
I think the problem is with Windows, for example in this browser if I hold down shift my mouse wheel does not work. So no I have not found a solution.
Your answer