- Home /
Transform.RotateAround over specific length of time
I'm using Transform.RotateAround to rotate an object (directional light) around the center of the game world, to simulate a day/night cycle.
The trouble I'm having is I need the complete 360-degree rotation to take exactly 1140 seconds, so essentially I need the rotation to occur at 0.3157894736842105 per second.
So far my code looks like this (clockSpeed is a variable that determines the speed of the clock)
transform.RotateAround(Vector3.zero, Vector3.forward, (cm.clockSpeed * 0.3157894736842105f) * Time.deltaTime );
This feels close but it's off somewhere, out of sync. If anybody could point me in the right direction, I'd greatly appreciate it.
Edit: clockSpeed is a variable that determines how many game-clock 'minutes' are passed with each second. The clock is more or less the following code:
void updateGameClock() {
if (clockRunning) {
cmtime = cmtime.AddMinutes(clockSpeed);
clock.text = cmtime.ToString("dd MMM hh:mm tt");
}
}
void startGameClock() {
clockRunning = true;
clockSpeed = 1f;
InvokeRepeating("updateGameClock", 1, 1F);
}
What value does cm.clockSpeed have? The out of sync might be from that value because you should only need to multiply your degree value with Time.deltaTime.
Edit: Did not actually see that you mentioned what the clockSpeed was. Sorry about that.
$$anonymous$$aybe you could give some more details on how you feel it is out of sync? What exactly is off with it? Has it not done 360 degrees after 1140 seconds? Has it done more or less? Or is it not moving smoothly?
Also: $$anonymous$$aybe ins$$anonymous$$d of entering the value directly you could calculate it before in the Start function?
float degrees;
private void Start()
{
degrees = 360 / 1140.0f;
}
I have a DateTime object that I use as a 'world' clock, that translates 1 second into 1 $$anonymous$$ute. I multiplied clockSpeed by 10 and watched a few rotations, and the same point was reached at different times.
The gameclock I've created shows the light reach the same point at different times during the 'day'. The 'sunset' occurs at different times each 'day', essentially. I've included the basic code I'm using for the clock for reference, in case it's a matter of that being off.
Answer by robertbu · Jan 06, 2014 at 05:40 PM
When you do something incrementally, there is a chance for the imprecision of floating numbers to compound. Since you are rotating around Vector3.zero, maybe you want to do an absolute axis rotation instead:
using UnityEngine;
using System.Collections;
public class Bug25ad : MonoBehaviour {
private float timestamp;
void Start() {
timestamp = Time.time;
}
void Update() {
float angle = (Time.time - timestamp) / 1140.0f * 360.0f;
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
}
}
Note it is unlikely you will get an Update() call at precisely 1140.0f seconds.
Thanks, but that doesn't seem to do it either. I think there's a difference with 'orbiting' the light source properly too, as opposed to it simply rotating where it stands.
I've added the basic clock code above, as that is how I'm testing it.
Sorry I didn't explain. The above code goes on an empty game object at Vector3.zero. You make the object that orbits a child object offset however much you want for the orbit.
Here is another short script. It assumes you are rotating around Vector3.zero and that the object rotating starts with a rotation of (0,0,0):
using UnityEngine;
using System.Collections;
public class Bug25b : $$anonymous$$onoBehaviour {
private Vector3 vOffset;
private float timestamp;
void Start() {
vOffset = transform.position;
timestamp = Time.time;
}
void Update () {
float angle = (Time.time - timestamp) / 1140.0f * 360.0f;
Quaternion q = Quaternion.AngleAxis(angle, Vector3.forward);
transform.position = q * vOffset;
transform.rotation = q;
}
}
No matter what solution you elect to use, avoiding incremental rotation and going with an absolute rotation calculation each frame will produce a more accurate result.
I'm very sorry, it's terribly embarrassing but the value 1140 needed to be 1440 actually, I mistyped it when writing the question! It syncs nicely, and is easily tied in with the clock system. Thanks mate!
Your answer
Follow this Question
Related Questions
Mouse control rotate around player 0 Answers
Rotate object around local Z axis 1 Answer
Proper way to rotate an object? 1 Answer
Quaternion, eulerAngles, localEulerAngles Driving me mad 1 Answer
rotate around character 1 Answer