• 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 /
This question was closed May 27, 2011 at 06:58 AM by biohazard for the following reason:

Reformulated better in new question.

avatar image
0
Question by biohazard · May 24, 2011 at 01:13 PM · javascripttime

Need help with this Rotation Script (fixes)

Hello,

I'm making my dashboard and successfully completed the alorithms to rotate my needles.

I wish to rotate them a variable amount of degrees WITHIN 1.5 Seconds.

Is it possible to use the Time class for t$$anonymous$$s?

i don't want my needles jump from position to position, i want them to rotate!

can somebody help me? i didnt find anyt$$anonymous$$ng useful in the scripting reference

EDIT : Script incoming, take cover :O

 var Source : TextAsset;
 
 var ZeigerSpeed : GameObject;
 var ZeigerRPM   : GameObject;
 var ZeigerHitze : GameObject;
 
 
 var DrehungSpeed = 0.0;
 var DrehungRpm = 0.0;
 var DrehungHitze = 0.0;
 
 var DrehungSpeed_alt = 0.0;
 var DrehungRpm_alt = 0.0;
 var DrehungHitze_alt = 0.0;
 
 
 
 
 
 
 
 
 
 
 function Dashboard()
 {
     var lines = Source.text.Split("\n"[0]);
     
     for(var i = 4; i < lines.Length; i++)
     {
         var values = lines[i].Split(";"[0]);
         
         DrehungSpeed = int.Parse(values[4]);
         DrehungRpm = int.Parse(values[8]);
         
         DrehungSpeed = (DrehungSpeed/300.0)*180.0;
         DrehungRpm = (DrehungRpm/8000.0)/240.0;        
         
         
         ZeigerSpeed.transform.Rotate(0,0,DrehungSpeed);
         ZeigerRPM.transform.Rotate(0,0,DrehungRpm);
         
         DrehungSpeed_alt = DrehungSpeed;
         DrehungRpm_alt = DrehungRpm;
         
         
         if(i>4)
         {
             DrehungSpeed = DrehungSpeed - DrehungSpeed_alt;
             DrehungRpm = DrehungRpm - DrehungRpm_alt;
         }
         
         if(DrehungSpeed < DrehungSpeed_alt)
         {
             DrehungSpeed = -(DrehungSpeed_alt - DrehungSpeed);
         }
         
         if(DrehungRpm < DrehungRpm_alt)
         {
             DrehungRpm = -(DrehungRpm_alt - DrehungRpm);
         }
         
         if(DrehungSpeed == DrehungSpeed_alt)
         {
             DrehungSpeed = 0;
         }
         
         if(DrehungRpm == DrehungRpm_alt)
         {
             DrehungRpm = 0;
         }
     }
 }
 
 function Start()
 {
     Dashboard();
 }
 
 function Update(){}    

EDIT: Rotation works now, inverting the values properly does not :/ i'm totally out of ideas

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 biohazard · May 26, 2011 at 11:20 AM 0
Share

getting an IndexOutOfRangeException

avatar image biohazard · May 26, 2011 at 01:58 PM 0
Share

The exception has been gotten rid of....

avatar image biohazard · May 26, 2011 at 02:22 PM 0
Share

Wow,any reason that people boycott my questions?

1 Reply

  • Sort: 
avatar image
1

Answer by Graham-Dunnett · May 24, 2011 at 01:23 PM

T$$anonymous$$s is just a maths question. At time now your needle is at angle theta1. In time now+1.5 you need your needle to be at angle theta2. So you need to rotate by (theta2-theta1)/1.5 degrees per second. Call t$$anonymous$$s deltaThetaPerSecond.

The Time Class has a member called deltaTime (http://unity3d.com/support/documentation/ScriptReference/Time-deltaTime.html). T$$anonymous$$s is documented as telling you how much time the last frame took to display. So, multiply deltaThetaPerSecond by Time.deltaTime and t$$anonymous$$s tells you how many degrees you need to rotate your needle by in the current frame.

After 1.5 seconds you no longer need to make t$$anonymous$$s change, so, in your Update() function after you have counted 1.5 second have passed just set deltaThetaPerSecond to zero. Now your needle won't rotate any more.

If you happen to get a new speed value inside the 1.5 second period, just re-compute deltaThetaPerSecond from the current angle and the new desired angle.

 var currentAngle = 0.0; // t$$anonymous$$s is the rotation of the needle in degrees
 function lese_datei()
 {
   for (;;) // do your current looping stuff
   {
     //csv reading stuff and compute desiredAngle
     deltaThetaPerSecond = (desiredAngle - currentAngle) / 1.5;
     yield WaitForSeconds(1.5);
   }
 }
 function Update()
 {
   var deltaAngle = deltaThetaPerSecond * Time.deltaTime;
   currentAngle += deltaAngle;
   Umdrehungen.transform.Rotate(0,0,currentAngle);
 }

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 biohazard · May 24, 2011 at 01:25 PM 0
Share

Oh, Hey Graham! Well, i'm not using Update() but i'll edit my Question to make u see the script

avatar image Graham-Dunnett ♦♦ · May 24, 2011 at 01:33 PM 0
Share

Sure, but the problem with the code you posted is you have a yield WaitForSeconds(1.5). This tells the code to not do anything for the next second and a half. If you want something to happen slowly over those 1.5 seconds you'll need to tell Unity what to do. I guess the simplest thing is to use an Update and use your lese_datei() function to compute deltaThetaPerSecond as per my answer. Then you'll make the calls into Umdrehungen.transform.Rotate and Geschwindigkeit.transform.Rotate in Update().

avatar image biohazard · May 24, 2011 at 01:36 PM 0
Share

i don't seem to get it. can you possibly post a little snippet for me to orientate? i've just realized that my code needs logical fixes anyway.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

[SOLVED]Possible Alternation of Transform.Rotate 1 Answer

Sorting through the same variable attached to several GameObjects from least to greatest, then returning an enum state based on that order 0 Answers

Convert Timer to HH:MM:SS 1 Answer

Randomly Generated Objects 1 Answer

Items with Statistics(such as attack damage) that actually effect the character? 2 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