Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
18
Question by e-bonneville · Mar 19, 2010 at 10:48 PM · mousezoomscroll-wheel

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

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image · Sep 26, 2010 at 11:06 PM 1
Share

Did you ever get this solved? The curse of getting an answer, even if it's not the one you're after...

avatar image e-bonneville · Feb 28, 2011 at 09:48 PM 0
Share

I finally did. Yeah, it's awful. :)

4 Replies

· Add your reply
  • Sort: 
avatar image
54
Best Answer

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.

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image e-bonneville · Feb 28, 2011 at 09:47 PM 0
Share

Yup, that's what worked for me in the end. Thanks for answering!

avatar image Jim-Groth · Aug 16, 2012 at 04:37 PM 0
Share

Works like a charm. Got myself a complete orbit camera now. :)

avatar image Unnatural Selection · Jul 15, 2015 at 03:38 PM 0
Share

How would I be able to make it do something different depending on if it's scrolling up or down?

avatar image Bunny83 · Jul 16, 2015 at 02:20 PM 5
Share
@unnatural-selection: GetAxis returns the "delta" value. So a number that represents how the axis has changed in relation to the last frame. This "change" can be positive or negative which usually relates to scroll up or down. // C# or UnityScript var d = Input.GetAxis("$$anonymous$$ouse ScrollWheel"); if (d > 0f) { // scroll up } else if (d < 0f) { // scroll down }
avatar image someguywithamouse · Jul 26, 2018 at 03:32 PM 0
Share

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.)

avatar image Davinder_Singh someguywithamouse · Mar 29, 2019 at 07:28 AM 0
Share

yes, you can change the name in project settings-> input manager

avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image e-bonneville · Mar 20, 2010 at 02:19 PM 0
Share

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.

avatar image kifcaliph · Nov 20, 2012 at 03:54 PM 0
Share

do you know how to keep the zoom value in that??

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by ratneshpatel · Feb 13, 2020 at 01:53 PM

Just to add to the answer given by @Bunny83 The output of the Input.GetAxis("Mouse ScrollWheel") would be = n * sensitivity // n is number of times the wheel is rotated in snaps/ It can be positive or negative // sensitivity is defined in the Input Settings. Default is 0.1f Other remarks: You can rename the name "Mouse ScrollWheel" to something else in the Input settings. For that you have to select MouseMovement as Input type and choose ThirdMovement
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Camera Zoom - FoV/Mouse Sensitivity Issues 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges