- Home /
Quaternions, Eulers, and Small Float Values?
I have this itty bit of code…
using UnityEngine;
using System.Collections;
public class PlanetManager : MonoBehaviour {
void FixedUpdate()
{
rotateSun();
}
float degreesRotation;
//rotate sun
void rotateSun()
{
//Y degrees the ratio between every angle to every second in a day
degreesRotation = 360.0f / ( Time.deltaTime * GameTime.speedOfDay * 86400.0f ); //0.004166667
//rotate the sun by Y degrees for every second that passes
print (degreesRotation);
Quaternion rotation = Quaternion.AngleAxis( degreesRotation, Vector3.right );
transform.rotation *= rotation;
// transform.Rotate(Vector3.right * degreesRotation);
// print(transform.rotation);
// print(rotation);
}
}
My issue lays in the fact that what degreesRotation spits out seems too small a value for any effect on the rotation - the object stays at it's same angle and doesn't move. If I test with a magic number like 0.05f it works, but if I add one more decimal point like 0.005f I seem to get nothing again. I've tested this with a few alternatives, as you can see, some in Quaternion (like Quaternion.Euler) since gimbal lock is a fear, and some with transform.Rotate / adding new vectors bit by bit.
Is my math wrong? or is this simply a 'thing' with these functions?
I cannot create a simple reproduction of your issue. Are you sure that the rotations you are entering are big enough to have a visual effect frame to frame? I thought there my be some sort of rounding going on, so I tried this bit of code:
#pragma strict
private var rot = 0.000005;
function Update() {
for (var i = 0; i < 10000; i++) {
//transform.Rotate(0.0, rot, 0.0);
transform.rotation = Quaternion.AngleAxis( rot, Vector3.up) * transform.rotation;
}
}
It applies a very small rotation 10,000 times per frame. I tried with both both the comment out line as well as the AngleAxis() line. Both worked fine. Pushing to 100,000 iterations with a 1/10 smaller value had the frame rate dramatically dropping, but the object did continue to rotate. So entering small values in the routines does not appear to be your issue.
Took a second look. I don't know the value of GameTime.speedOfDay, nor do I know what 86400 represents, but if the whole line sets 'degreesRotation' to 0.0042 as suggested by the comment, then the object will rotate a about a degree every four seconds (when running on average at 60 FPS). So I tried it with:
#pragma strict
private var rot = 0.004;
function Update() {
transform.rotation = Quaternion.AngleAxis( rot, Vector3.up) * transform.rotation;
}
And I got an about one degree every four seconds rotation.
Tried that, changed Vector3.up to Vector3.right (so X axis ins$$anonymous$$d of Y) and it still won't move, however if I have it with your setup, on the Y axis, it will. The current light this is applied to is at -90 degrees on the X axis. If I change it to zero, for example, it does work, so it sounds like gimbal lock, but with a Quaternion?
Also, that would mean my math is definitely wrong. I'm trying to get this light to move N amount of angles per 1 second and smoothed out accordingly (via Time.deltaTime). Of course, to GameTime one second is not a constant 1 but ins$$anonymous$$d a the result of this expression
seconds += speedOfDay * Time.deltaTime;
With a clause to reset after it has been 86400 seconds, or 1 day. The system works great, and it pairs up exactly with a stopwatch outside of the system, but that leaves me with trying to figure how how to make the sun move in accordance to time. Any ideas? Thanks for your time and words.
Is this just a local vs. world axis issue? What happens if you use transform.Rotate() ins$$anonymous$$d of Quaternion.AngleAxis()?
@imarriedyoung - why do you use FixedUpdate and not Update?
Your answer
![](https://koobas.hobune.stream/wayback/20220613115558im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Quaternion reset rotations didnt smooth 0 Answers
Object doesn't rotate properly 1 Answer
Rotating an object in relation to its endpoints 1 Answer
How to steer a vehicle 1 Answer