Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by Seresia · Feb 22, 2013 at 04:01 PM · mousecontrolzoom

Zoom in/out with right button of mouse

Hi all,

I started in unity.

I am looking for a way to zoom in and zoom out with the mouse button. The goal is to run a game without using the mouse scrollwheel.

I started with the following code but I'm stuck.

 function LateUpdate () {
     if (target && Input.GetMouseButton(0))  {
         x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
         y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
 
         y = ClampAngle(y, yMinLimit, yMaxLimit);
 
         var rotation = Quaternion.Euler(y, x, 0);
         var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
 
         transform.rotation = rotation;
         transform.position = position;
     }

//HERE//

       if (Input.GetMouseButtonDown(1)) {           
            distance += Input.GetAxis("Mouse X") * xSpeed * 0.001;                            
            transform.Translate(Vector3.forward * -zoomSpeed); 
     }
  
      if (Input.GetMouseButtonUp(1)) {     
                distance -= Input.GetAxis("Mouse Y") * ySpeed * 0.001;                             
                transform.Translate(Vector3.forward * zoomSpeed); 
 }
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 sdgd · Feb 22, 2013 at 05:27 PM 0
Share

well give axis gives -1 || 0 || 1 not anything else

 if (Input.Get$$anonymous$$ouseButtonUp(1)) {
     $$anonymous$$ove$$anonymous$$e += (Input.GetAxis("$$anonymous$$ouse Y"));
     transform.Translate (0,0, $$anonymous$$ove$$anonymous$$e * Time.deltaTime);
 }

hope helps

avatar image Seresia · Feb 23, 2013 at 10:24 AM 0
Share

Thank you for your help.

So your code works but it is not what I'm looking. With your code and clicking the right button of the mouse, I zoom forward in clicks.

In fact that research and to give a specific example, it is the same effect as in magnifying glass zoom Photoshop CS6, zoom in and zoom out power while keeping the right click of the mouse without clicking (and thus a movement fluid zoom) according to the direction of the mouse.

If I slide the mouse up while keeping the right mouse button, zoom it and vice versa, if I slide the mouse down while keeping the right mouse button I zoom out.

Here is the complete code with you code :

 var target : Transform;
 var distance = 20.0;
 var $$anonymous$$ove$$anonymous$$e = 5;
 
 var xSpeed = 150.0;
 var ySpeed = 80.0;
 
 var y$$anonymous$$inLimit = 20;
 var y$$anonymous$$axLimit = 80;
 
 // Augmenté:
 var maxDist : float = 20;
 var $$anonymous$$Dist : float = 5;
 var zoomSpeed : float = 5;
 // ici
 
 private var x = 0.0;
 private var y = 0.0;
 
 @script AddComponent$$anonymous$$enu("Camera-Control/$$anonymous$$ouse Orbit")
 
 function Start () {
     var angles = transform.eulerAngles;
     x = angles.y;
     y = angles.x;
 
     // $$anonymous$$ake the rigid body not change rotation
     if (rigidbody)
         rigidbody.freezeRotation = true;
 }
 
 function LateUpdate () {
     if (target && Input.Get$$anonymous$$ouseButton(0))  {
         x += Input.GetAxis("$$anonymous$$ouse X") * xSpeed * 0.02;
         y -= Input.GetAxis("$$anonymous$$ouse Y") * ySpeed * 0.02;
 
         y = ClampAngle(y, y$$anonymous$$inLimit, y$$anonymous$$axLimit);
 
         var rotation = Quaternion.Euler(y, x, 0);
         var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
 
         transform.rotation = rotation;
         transform.position = position;
     }
     // Zoom in??
         if (Input.Get$$anonymous$$ouseButtonUp(1)) {
             $$anonymous$$ove$$anonymous$$e += (Input.GetAxis("$$anonymous$$ouse Y"));
             transform.Translate (0,0, $$anonymous$$ove$$anonymous$$e * Time.deltaTime);
 }
 
     // Zoom out??
 }
     
 
 
 static function ClampAngle (angle : float, $$anonymous$$ : float, max : float) {
     if (angle < -360)
         angle += 360;
     if (angle > 360)
         angle -= 360;
     return $$anonymous$$athf.Clamp (angle, $$anonymous$$, max);
 }

1 Reply

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

Answer by AlucardJay · Feb 25, 2013 at 06:22 AM

Here is an example script showing 2 ways to check for changes in the mouse while the RMB is pressed.

The first basically reads the Input Manager value for Input.GetAxis( "Mouse X" )

The second calculates the changes in mouse position by remembering where it was last frame, then subtracting that from the current position.

Create a new scene, and attach this script to an empty gameObject. Hit Play, hold the RMB and move the mouse, check the output in the console. This is showing Input.GetAxis( "Mouse X" ). Now while still playing, change the boolean to false. Hold RMB and move mouse, now the console is displaying the calculations for the delta method.

Input.mousePosition gives screen coordinates, I have no clue what GetAxis returns, but that explains the difference in the values for the same movement. EDIT : (with help from fafase) the Unity Scripting Reference states If the axis is setup to be delta mouse movement, the mouse delta is multiplied by the axis sensitivity and the range is not -1...1.

You can fix the values from Input.mousePosition to a normalized value (which I recommend) by dividing the delta x and y by Screen.width and height. I have included this i the function, that is why there are 2 debugs : before and after normalization, normalize usually means a value between 0 and 1 (0 nothing, 1 maximum possible).

SO first calculate a value based on changes in the mouse using one of the methods I have shown, then use that value in the modification of your camera position =]

 #pragma strict
 
 var quickMethod : boolean = true;
 
 // ----
 
 function Update() 
 {
     if ( Input.GetMouseButton(1) )
     {
         if ( quickMethod )
         {
             GetMouseX(); // quick method
         }
         else
         {
             CalculateMouseDelta(); // longer method
         }
     }
 }
 
 // ----
 
 var axisDelta : Vector3 = Vector3.zero;
 
 function GetMouseX() 
 {
     if ( Input.GetAxis( "Mouse X" ) )
     {
         Debug.Log( "Input.GetAxis( \"Mouse X\" ) = " + Input.GetAxis( "Mouse X" ) );
         
         axisDelta.x = Input.GetAxis( "Mouse X" );
     }
     
     if ( Input.GetAxis( "Mouse Y" ) )
     {
         Debug.Log( "Input.GetAxis( \"Mouse Y\" ) = " + Input.GetAxis( "Mouse Y" ) );
         
         axisDelta.y = Input.GetAxis( "Mouse Y" );
     }
 }
 
 // ----
 
 var lastPos : Vector3 = Vector3.zero;
 var mouseDelta : Vector3 = Vector3.zero;
 
 function CalculateMouseDelta() 
 {
     var currPos : Vector3 = Input.mousePosition;
     
     mouseDelta = currPos - lastPos;
     
     Debug.Log( "mouseDelta.x = " + mouseDelta.x + " : mouseDelta.y =  " + mouseDelta.y );
     
     lastPos = currPos;
     
     // - Normalize this input -
     
     mouseDelta.x = mouseDelta.x / Screen.width;
     mouseDelta.y = mouseDelta.y / Screen.height;
     
     Debug.Log( "NORMALIZED   mouseDelta.x = " + mouseDelta.x + " : mouseDelta.y =  " + mouseDelta.y );
 }
 
 // ----
Comment
Add comment · Show 7 · 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 AlucardJay · Feb 25, 2013 at 06:55 AM 0
Share

hee hee, thanks dude =]

What I was referring to explicitly was how is this value calculated? If you move the mouse quick enough with the above script, axisDelta x and y returns values above 1, therefore are not normalized. I assumed it would say "ok, i can move a maximum of Screen.width, the mouse moved x% of that amount, so I shall return x/Screen.width". Unless the mouse is moved for a distance larger than scrolling the mouse cursor on the screen allows that's why it can return > 1

avatar image fafase · Feb 25, 2013 at 06:58 AM 0
Share

Yep I was wondering how you would not know how GetAxis works...

avatar image AlucardJay · Feb 25, 2013 at 07:03 AM 0
Share

lol, never used it for mouse myself. If all else fails, read the API hey ! Here is the information for future readers (answer also updated) :

If the axis is setup to be delta mouse movement, the mouse delta is multiplied by the axis sensitivity and the range is not -1...1.

http://docs.unity3d.com/Documentation/ScriptReference/Input.GetAxis.html

"Look mom, I learned something today !"

avatar image Seresia · Feb 25, 2013 at 10:08 AM 0
Share

Thank you for you help, I try this!

avatar image AlucardJay · Feb 25, 2013 at 12:05 PM 0
Share

No worries, but ....

Please don't post comments as answers. Post comments by clicking the [add new comment] button, a window then open for you to type in. Answer fields are for answers only, as this is a knowledge base.

Here at Unity Answers, Answer means Solution, not Response.

Show more comments

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

12 People are following this question.

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

Related Questions

How to make camera position relative to a specific target. 1 Answer

Is it possible to add zoom to the mouse orbit script? 2 Answers

How do I use the mouse to control a spacecraft like in this game? 1 Answer

Controlling Players Mouse 2 Answers

Turn-based strategy game 2 Answers


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