- Home /
Rotate object about x-axis with mouse like in editor
I want to rotate an object like one can rotate an object in the editor.
The user performs a click on the object (anywhere). By holding the mouse button down she is then able to rotate the wheel (see image) about its x-axis. The rotation should feel like the rotation one can perform in the unity editor.
How can I achieve that? Any Help is appreciated :)!
EDIT: The coordinates of the mouse position are given and can be used as Vector2 MousePos
.
Answer by robertbu · Mar 18, 2013 at 02:55 PM
The simplest is to use Input.GetAxis():
var sensitivity = 5;
function OnMouseDrag() {
transform.Rotate(Vector3(sensitivity * Input.GetAxis("Mouse Y"), 0, 0));
}
But you can use mousePos. I've use ScreenToViewportPoint() to make the code resolution indenpendent. It is not necessary. Replace with direct assignment and adjust the sensivity way down.
var sensitivity = 300.0;
private var lastPos : Vector3;
function OnMouseDown() {
lastPos = Camera.main.ScreenToViewportPoint(Input.mousePosition);
}
function OnMouseDrag() {
var pos = Camera.main.ScreenToViewportPoint(Input.mousePosition);
transform.Rotate((pos.y - lastPos.y)*sensitivity, 0, 0);
lastPos = pos;
}
As simple as it is. It works. But I have one issue. When rotating the object with the mouse I loose the object, meaning: The object rotates too slow compared to my mouse movement.
I know I can adjust sensitivity
but putting in a value of >500 of even >1000 doesn't seem like an elegant solution. Is there a better way to fit mouse movement to the rotation speed (regardless of screen solution)? Or is the sensitivity solution the best practice? Thank you :)!
Setting the sensitivity is just fine unless the object is moving relative to the camera (i.e. it is a knob in the scene that a character can be difference distance from). In that case I'd use world coordinates. Note if you were dragging an object rather than rotating, there would be a correct way of marrying the mouse to the object. But in rotating, for me, it is just what feels right.
So I guess sensitivity is the best practice for this kind of object manipulation. Is the sensitivity value dependent on the screen resolution?
In fact the player can be at difference distance and angles from the object. In which way must the code be modified to allow object rotation from any distance and especially from any angle?
In the second code above, sensitivity is not related to screen resolution as long as the aspect of the game remains the same. Viewport coordinates start at (0,0) in the lower left and go to (1,1) in the upper right no matter what the resolution of the screen. If you end up with an aspect change, there will be some change in the X sensitivity, but not the 'Y'.
If you have a scene object, then you could use world coordinates ins$$anonymous$$d of viewport coordinates. There are several ways to do this. One way is to do calculate the two positions above (lastPos and pos) using this code (untested):
var dist = $$anonymous$$athf.Abs(Vector3.Distance(target.transform.position - Camera.main.transform.position)).
var pos = Input.mousePosition;
pos.z = dist;
pos = Camera.main.ScreenToWorldPoint(pos);
Thank you for the world coordinate solution. I will give it a try and will tell you if it has worked :).
Your answer
Follow this Question
Related Questions
Rotation of Object on single axis in direction of the mouse position 0 Answers
Slerp/Rotational Problem Query 1 Answer
Object Look At Mouse 2 Answers
Smooth camera rotation on mobile and pc when GetMouseButton()? 0 Answers
Problem with rotation script. 3 Answers