- Home /
Implementing the ScrollWheel:
How would I implement the ScrollWheel
? I've got a vague idea of using the ScrollWheel
to make a zoom function in an FPS game. I have a zoom of sorts working, where you press ALT and the field of view decreases, but I would like to use the ScrollWheel
instead. Thing is, I don't know how to get scroll events, which might hinder my development. If I could get the scroll amount, I could add or decrease the field of view until it reaches a zoom limit. In theory, that should work. Any and all help appreciated.
Thanks in advance - Elliot Bonneville
Did you ever get this solved? The curse of getting an answer, even if it's not the one you're after...
Answer by Bunny83 · Feb 26, 2011 at 02:36 AM
devilkkw's answer shows the right thing, but saying "simple code" and then post a script with 98% unrelated stuff is not really a good answer.
What you actually need, i guess you've found it already, is Input.GetAxis("Mouse ScrollWheel")
. It returns the delta movement that happend in the last frame. It can be positive or negative (up/down).
Make sure that the axis is setup in the input manager and named accordingly. The default "Mouse ScrollWheel" is of type "Mouse Movement" and uses the 3rd axis which is normally the scrollwheel.
Yup, that's what worked for me in the end. Thanks for answering!
Works like a charm. Got myself a complete orbit camera now. :)
How would I be able to make it do something different depending on if it's scrolling up or down?
Does it HAVE to be named $$anonymous$$ouse Scrollwheel? I'm pretty sure you should be able to use a custom name too. (As long as you're using the same name in your script.)
yes, you can change the name in project settings-> input manager
Answer by devilkkw · Mar 20, 2010 at 11:06 AM
this is simply code to do:
var target : Transform; var distance = 10.0;
var xSpeed = 100.0; var ySpeed = 100.0;
var yMinLimit = 2; var yMaxLimit = 80;
var distanceMin = 3; var distanceMax = 25;
private var x = 0.0; private var y = 0.0;
function Start () { var angles = transform.eulerAngles; x = angles.y; y = angles.x;
if (rigidbody)
rigidbody.freezeRotation = true;
}
function LateUpdate () { if (target) { x += Input.GetAxis("Mouse X") xSpeed distance* 0.02; y -= Input.GetAxis("Mouse Y") ySpeed 0.02;
y = ClampAngle(y, yMinLimit, yMaxLimit);
var rotation = Quaternion.Euler(y, x, 0);
distance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel")*5, distanceMin, distanceMax);
var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
transform.rotation = rotation;
transform.position = position;
}
}
static function ClampAngle (angle : float, min : float, max : float) { if (angle < -360) angle += 360; if (angle > 360) angle -= 360; return Mathf.Clamp (angle, min, max); }
attach on our camera and select target.
Nope. That isn't doing it - I'm not sure what that actually is doing... Is it supposed to zoom in on a target? I think I've been misunderstood. Thanks for the reply anyway, though.
Answer by BillyBobBeavis · Dec 05, 2018 at 07:14 PM
int travel;
int scrollSpeed = 3;
void Update()
{
var d = Input.GetAxis("Mouse ScrollWheel");
if (d > 0f && travel > -30)
{
travel = travel - scrollSpeed;
Camera.main.transform.Translate(0, 0, 1 * scrollSpeed, Space.Self);
}
else if (d < 0f && travel < 100)
{
travel = travel + scrollSpeed;
Camera.main.transform.Translate(0, 0, -1 * scrollSpeed, Space.Self);
}
}
This code worked best for my purposes. It's a simple forward/backward movement with limits. The limits are relative to the camera's original position.
Answer by ratneshpatel · Feb 13, 2020 at 01:53 PM
Your answer
Follow this Question
Related Questions
Having problems getting a zoom to mouse function working well with smoothstep, need some help. 0 Answers
How can I stop the middle mouse button from zooming out when I single click it inside the editor? 2 Answers
Zoom in/out with right button of mouse 1 Answer
Re-center UI Image after zooming out 0 Answers