Hi, I wonder if anyone can help me with this script please?
I want to be able to rotate an object with the mouse (think Google Earth, when you first start and can freely rotate the earth around its axis with the mouse).
however, the problem with the above is that it won’t fully turn the object around (it stops at 180 degrees) - but I want to be able to keep rotating with more drags on the mouse.
Also, when using the above, if I turn the object around a bit, the mouse movements no longer rotate from the camera’s perspective. I really want to be able to always turn the object in the direction of the mouse-drag, irrespective of the rotation that the object has prior to the drag.
Ultimately, I would like to be able to spin the object in a natural way so that when you let go of the mouse-drag it continues to spin around in the direction of the drag, with a slowing friction…
Anyone have any suggestions - would be grateful of any advise please…
First off I would put the code in Update not in OnGUI.
Here’s a couple of ways I use. This method moves the camera not the object but the result is the same. I’m sure you could do something very similar to rotate the object and not the camera. The VR trackball is more prone to running at different speeds depending on the speed of the machine. The second way is more stable but doesn’t give the same trackball effect. Actually I rather prefer it.
I had to cut this code out of a larger piece so its not guarenteed to work unaltered, but you should get the idea
private var lastV : Vector3 = new Vector3(0,0,0);
var xSpeed = 200.0;
var ySpeed = 200.0;
var zSpeed = 40.0;
var trackballSpeed = 1.3;
var VRTrackBall : boolean = true;
private var v1 : Vector3;
private var v2 : Vector3;
private var vrAxis : Vector3;
private var storeCamera : Quaternion;
if (Input.GetMouseButtonDown(0)) {
v1 = vrTrackBallPos(Input.mousePosition);
lastV = v1;
storeCamera = transform.rotation;
lastMousePos = Input.mousePosition;
}
var offset = Input.mousePosition - lastMousePos;
if (VRTrackBall) {
vrAxis = Vector3.Cross(v1, v2);
var theta : float = Vector3.Angle(v1, v2);
var rotDelta : Quaternion = Quaternion.AngleAxis(theta*trackballSpeed, vrAxis);
transform.rotation = storeCamera.rotation * rotDelta;
}
else {
x = offset.x * xSpeed * 0.002;
y = offset.y * ySpeed * 0.002;
var rotDeltaX : Quaternion = Quaternion.AngleAxis(x, Vector3.up);
var rotDeltaY : Quaternion = Quaternion.AngleAxis(y, Vector3.left);
transform.rotation = storeCamera.rotation * rotDeltaX * rotDeltaY;
}
There’s a camera control script that comes with Unity that you can also use but I found that again it ran at different speeds on different machines and nothing I did would alter that even including Time.deltaTime which should compensate for it. If you use that script then just remove the clamping function that stops it spinning all the way around.