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 HAWK7 · Jan 26, 2014 at 03:08 PM · rotatetargetrotatearoundrotating

Rotate exactly on time

I need to make a cube rotate to the beat for a music video. The song is at 120 bpm.

one phrase consists out of 4 beats so that the phrase is 2 seconds long.

The first beat hits at 0, the second at 0,25, the third at 0,75 and the last at 1.

With only a little bit of math you know that the beats last 0.25, 0.5, 0.25 and 1 seconds.

After every beat the cube has be rotated 90 degrees. I calculated the speed for each rotation.

But with my solution the rotation is not precise because the operation is in the update function, which varies with the frame rate.

It would be the best to stop a little bit when the cube rotated 90 degrees and then rotate it again, but with my solution it gets worse because the times get smaller and the precision worse.

 using UnityEngine;
 using System.Collections;
 
 public class rotate : MonoBehaviour 
 {
     private float speed1 = 360f;
     private float speed2 = 180f;
     private float speed3 = 360f;
     private float speed4 = 90f;
 
 
     void Update ()
     {    
         if (transform.rotation.eulerAngles.y < 90f)
             {
                 float dreh1 = Mathf.Clamp(speed1 * Time.deltaTime, 0f, 90f);
                 transform.Rotate(0, dreh1, 0);
             }
 
         if (90 <= transform.rotation.eulerAngles.y & transform.rotation.eulerAngles.y < 180f)
             {    
                 float dreh2 = Mathf.Clamp(speed2 * Time.deltaTime, 0f, 180f);
                 transform.Rotate(0, dreh2, 0);
             }
 
         if (180 <= transform.rotation.eulerAngles.y & transform.rotation.eulerAngles.y < 270f)
             {
                 float dreh3 = Mathf.Clamp(speed3 * Time.deltaTime, 0f, 270f);
                 transform.Rotate(0, dreh3, 0);
             }
         
         if (270 <= transform.rotation.eulerAngles.y & transform.rotation.eulerAngles.y < 360f)
             {
                 float dreh4 = Mathf.Clamp(speed4 * Time.deltaTime, 0f, 360f);
                 transform.Rotate(0, dreh4, 0);
             }
 
     }
 }
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by drew_wiskus · Mar 10, 2017 at 07:03 AM

You're probably going to want to look into fixed update.

 void FixedUpdate() {}

From the documentation: https://docs.unity3d.com/ScriptReference/MonoBehaviour.FixedUpdate.html

"This function is called every fixed framerate frame, if the MonoBehaviour is enabled."

//OR: You can do fancy caluclations with Time.time;

 private float start_time;

 void Start() {
     start_time = Time.time;
 }

 void Update() {
     //fancy calculations with start_time vs Time.time;
     float current_time_in_song =  Time.time - start_time;
 }
Comment
Add comment · 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

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

Rotate Z axis of object to camera 2 Answers

Image Rotate Gallery Snap 0 Answers

rotating player 180 degrees 0 Answers

Rotate around obsolete - missing Rotate overloads??? 2 Answers

Strange rotation pattern. 0 Answers


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