# I would like to do this...

Hi guys, I’d like to click and drag on an object to make it spin. I am on my third day on Unity :shock: so this is what I had in mind. I could be completely way of the mark.

OnMouseDown: I would get the objects local upVector in worldspace (localToWorldMatrix in transform component, I think). Then I would multiply that by the camera’s worldToCamera Matrix.

OnMouseUp: I would get the mouse delta vector and calculate an angle.

Does it sound right? OR do I have to project those vector unto the screen? I have no idea how to begin :shock:

I found a much simpler way of doing this!

transform.TransformDirection

//calculate x axis relative to camera
camera = Camera.main.transform;
cameraRelativeRight = camera.TransformDirection(Vector3.right);

done (practically) :lol:

I am more impressed with Unity3D everyday.

While you could do something like this, to simply spin an object around one of its axes have a look at Transform.Rotate as a simpler alternative. You can even set whether it rotates in local space or world space depending on what you want.

As for the angle, if you want the object to ‘spin’ rather than just rotate to a position and stop you’re probably better off thinking in terms of rotation speed rather than angles. By that I mean you want the mouse movement to determine how fast, not how far to rotate. Something like this:

rotation += Input.GetAxis("Mouse X") * rotateSpeed;

inside a function like Update() or OnMouseDrag() where it will loop to continuously update the rotation.

Hey DawVee,
I think that would definitely work with the speed and rotation. The tricky thing is the camera may have rolled up to 60 degrees. I thought if I could convert the drag direction to worldSpace or conversely, convert the object local upVector to camera space, I could execute a rotation for the player intuitively by dragging on the object. Also,
I was going to clamp those directions to 4 orthogonal rotations in pos and negative directions along the closest axis to drag direction. I hope that makes sense.

Ah, so you want the object to rotate in x and y relative to the camera, regardless of the object’s orientation or the camera’s orientation to the world, have I got that right?

In any case where you’re looking at rotation around an arbitrary axis, Quaternions are (oddly enough) the simplest method. So in this case you could probably use the class method Quaternion.AngleAxis to construct the rotation, with the camera’s transform.up property as the first rotation axis, and then construct a second rotation with the camera’s transform.right property as the second rotation axis. Then you just multiply your object’s transform.rotation property by the new Quaternion.

So your script would look something like:

xRotation += Input.GetAxis("Mouse X") * rotateSpeed;

var newXRotation : Quaternion = Quaternion.AngleAxis(xRotation, camera.transform.up);

yourObject.transform.rotation *= newXRotation;

and repeat the same again for the other axis.

Thank you dawvee, that is brilliant. and so concise :shock: