- Home /
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);
}
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
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);
}
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 );
}
// ----
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
Yep I was wondering how you would not know how GetAxis works...
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 !"
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.