- Home /
Rotating On Two Axes Independantly
Ok, so this could be hard for me to explain, but I will try my best and make use of extensive images. This is a 3-Dimensional math problem, and I am having trouble finding the answer.
So basically, I have written a script that creates a "ring" of orbiting GameObjects (in my example, several Spheres) that are instantiated as a Child under a central GameObject (in this example, a Cube).
The cube's origin is 0,0,0 with no rotation. The spheres are generated equidistant from the origin Cube when given a distance (public int dist) at an angle calculated based on the number of objects being generated (public int num). This script is placed on the Cube object:
Vector3 origin = transform.position;
for (int i = 0; i < num; i++){
int angle = 360 / num;
int a = i * angle;
var pos = NewCircle(origin, dist, a);
GameObject newPrefab = Instantiate(prefab, pos, Quaternion.identity);
newPrefab.transform.parent = transform;
}
and
Vector3 NewCircle(Vector3 center, float radius, int a)
{
Debug.Log(a);
float ang = a;
Vector3 pos;
pos.x = center.x + radius * Mathf.Sin(ang * Mathf.Deg2Rad);
pos.y = center.y + radius * Mathf.Cos(ang * Mathf.Deg2Rad);
pos.z = center.z;
return pos;
}
So that's the basic creation code. It is built on the X and Y coordinates of the scene and it ends up looking like this:
The problem comes when I start rotating these objects.
The goal is for this object to rotate on the X and Z axes, with the Y rotation remaining unchanged (at 0) so when Trail Renderers are added to the Sphere objects, it creates a very particular pattern.
The problem is, I can't rotate on the X and Z axes without affecting the angle on the Y axis. I came up with a solution that technically achieves this. However, the Trail Renderers do not "trail" on the Y axis. Why? Because, instead, I have positioned the camera and rotated IT on the Y axis, which gives the illusion of the object rotating. However, while it shows the correct positioning and rotation of the object, the effect is not what I want. Rotation code:
void Update () {
targetRotation *= Quaternion.Euler(0, 0, 2);
transform.rotation= Quaternion.Slerp (transform.rotation, targetRotation , 10 * Time.deltaTime);
}
Basically, the "ring" rotates counter clockwise, on the Z axis (See previous screenshot), while the Camera, positioned under the ring, looking up (Orthographic; Rotation: Y=90;), rotates on its local Z axis, or rather, the Scene's X axis.
In-Game, the Object appears to be rotating along the Scene's X axis, but really is just the camera rotating:
So, as you can see above, the Trail Renderers do not render along the X rotational plane, because the object, is not rotating along it. However, If I add X rotation to the Objects rotational code:
void Update () {
targetRotation *= Quaternion.Euler(2, 0, 2);
transform.rotation= Quaternion.Slerp (transform.rotation, targetRotation , 10 * Time.deltaTime);
}
the rotational planes combine, and begin affecting the Y axis' angle of rotation. It needs to stay at 0.
I am sure someone smarter than I has a mathematical way of interpolating the Y axis to remain at 0. Any help is much appreciated.
Thank you in advance.
Your answer
Follow this Question
Related Questions
Rotate an object's yaw, pitch, and roll relative to the Game Camera Axis. 1 Answer
need my object to rotate between specific angles on the x axis 2 Answers
transform.right not working correctly? 0 Answers
Rotate an object such that it is theta degrees relative to another object 1 Answer
Rolling a capsule lengthways, sideways and twisting with AddTorque 1 Answer