- Home /
[Trackball] Rotation around 2 axis
Hi,
I'm trying to simulate a trackball by allowing a sphere to rotate around the x and y axis. I did a small piece of code to make my sphere rotate along those 2 axis simultaneously :
void Update ()
{
prevAngle.x += 5;
prevAngle.y += 5;
transform.eulerAngles = new Vector3(prevAngle.x%360,prevAngle.y%360,0);
}
I was expected my sphere to rotate in "diagonal" but it is not the case - the movement is very weird.
I modified a bit the previous code in order to rotate only along the x axis and I was surprised to notice via the inspector that the x rotation value is not the only one to be update, sometimes the y and z values jump to 180 and then back to 0.
I do want the x and y rotation values to be the only one to be modified and I want to be able to compare between two updates the delta angle for the x and y axis.
I tried to use the Quaternion classes with the eulerAngles but I got a new problem, the x value freeze at 90 - cannot understand why.
So how can I make my sphere rotate along the X and Y axis and be able to calculate the delta angle for each axis ?
Thank you
I think you need to move to relative rotations. Euler Rotations hard set like that won't give you what you want, (I'm imagining it looks a bit like a figure of 8 or a spiral, as you're effectively changing the axis with one and rotating with the other like seasons on earth).
transform.rotation=Quaternion.identity;
transform.rotate(new Vector3(prevAngle.x,pRevAngle.y,0),Space.World);
will give probably smooth rotations although the axes may be wrong(I suspect x and z). You may need to reposition your sphere afterwards (?). alternatively, it may be easier to use a use $$anonymous$$athf.Sin and $$anonymous$$athf.cos to calculate a position around the sphere which it then looks at (transform.LookAt(wherever,Vector3.up)).
Answer by maddFrogg · Apr 22, 2014 at 10:59 AM
Have you tried this?
http://docs.unity3d.com/Documentation/ScriptReference/Transform.Rotate.html
Answer by bali33 · Apr 22, 2014 at 11:09 AM
Yes I did. The problem is the same, when you Rotate only along the x axis the y and z angle values are set to 180 for a short times. It seems that this modification occur when the x angle is equal to 360.
this.transform.Rotate( .5f, .5f, 0 );
This code rotates my sphere quite good, I don't know hat happens to yours. Please explain the weird movement you are experiencing.
If I do this.transform.Rotate( .5f, .5f, 0 ) and then Debug the eulerAngle.x I can see that the value goes from 0 to 45. then to 0 then right after 0 the value change to 360 and then decreases etc. The x angle values update is weird, it's not like the value goes to 0 to 359 and then to 0 etc.
So it's not possible to compute and delta angle between two updates because sometimes you obtain strange values. I don't only want my trackball to rotate, I want to be able to know at each frame the angle delta for each axis in order to use those values to compute my main character speed for each axis.
Your answer