- Home /
Reformulated better in new question.
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 this?
i don't want my needles jump from position to position, i want them to rotate!
can somebody help me? i didnt find anything 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
Answer by Graham-Dunnett · May 24, 2011 at 01:23 PM
This 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 this deltaThetaPerSecond.
The Time Class has a member called deltaTime (http://unity3d.com/support/documentation/ScriptReference/Time-deltaTime.html). This is documented as telling you how much time the last frame took to display. So, multiply deltaThetaPerSecond by Time.deltaTime and this 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 this 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; // this 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);
}
Oh, Hey Graham! Well, i'm not using Update() but i'll edit my Question to make u see the script
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().
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.
Follow this Question
Related Questions
How to simplify my Equipment method? 1 Answer
[SOLVED]Possible Alternation of Transform.Rotate 1 Answer
Convert Timer to HH:MM:SS 1 Answer
Randomly Generated Objects 1 Answer