Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Ozale · Jan 06, 2014 at 12:50 PM · rotationrotaterotatearound

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);
     }
 
Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image GameVortex · Jan 06, 2014 at 01:55 PM 1
Share

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;
 }
avatar image Ozale · Jan 07, 2014 at 02:01 AM 0
Share

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.

avatar image Ozale · Jan 07, 2014 at 02:45 AM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Ozale · Jan 07, 2014 at 02:43 AM 0
Share

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.

avatar image robertbu · Jan 07, 2014 at 06:20 AM 1
Share

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.

avatar image Ozale · Jan 08, 2014 at 02:24 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges