- Home /
keep an if statement updating per frame in function Update
I'm trying to randomly change the speed of the rotation of an object in my scene. What I want is for the object to rotate at a speed of, lets say, 10 *Time.deltaTime and keep this speed for a set amount of time (again lets say 10(seconds)).
After the set amount of time has passed, I want the script to then calculate the next randomly selected speed for the rotation and to rotate the object at this speed for another randomly selected amount of time.
The script should then repeat randomly changing the speed of rotation and the time this speed lasts for the object.
This is what I have so far:
var myNumbers = 10;
function Update () {
var chance = Random.Range(0, 4);
//transform.Rotate(randomNumbers[-20], 0, 0);
if (chance == 1)
{
Debug.Log("RandomA is Go");
amtToMove = myNumbers * Time.deltaTime;
transform.Rotate(myNumbers*Time.deltaTime, 0, 0);
}
if (chance == 0)
{
Debug.Log("RandomB is go");
transform.Rotate(-10*Time.deltaTime, 0, 0);
}
if (chance == 2)
{
Debug.Log("RandomC is Go");
transform.Rotate(20*Time.deltaTime, 0, 0);
}
if (chance == 3)
{
Debug.Log("RandomD is Go");
transform.Rotate(-20*Time.deltaTime, 0, 0);
}
}
I know we can't yield in a function update, but is there anyway to keep the transform.Rotate continuing for a set amount of time, without calculating the next chance variable?
I'm sure I should be using arrays, but I can't quite figure out how to use them with integers...
Thanks in advanced, any help would be grateful.
Greg
This issue has been resolved, if anyone has a similar issue or questions about the code feel free to drop me a mail!
Greg
Answer by Bunny83 · Apr 12, 2011 at 05:49 PM
Your script looks really strange... I don't even get what it should do.
According to your explanation i would do something like that:
var nextChange : float = 0.0; var currentSpeed : float = 10.0;
function Update() { if (Time.time > nextChange) { nextChange = Time.time + Random.Range(2.0,10.0); // wait 2 - 10 sec. currentSpeed = Random.Range(2.0,10.0); } transform.Rotate(currentSpeed * Time.deltaTime, 0, 0); }
That actually makes a lot of sense, you have to excuse my code, I've spent the day getting the model finished and couldn't wrap my head around the logic of this. This works superbly though, I'll have to send you a link of the finished project when we're done over here, many thanks Bunny! Greg
No excuse, i admire people that work on both sides (art and code). I have absolutely no art skills, but sometimes it would be very useful...
Hey, couldn't find you email but hopefully this comment should trigger a message in your mail. Just a quick thanks and to show you the project we made within a 12 hour period.
The unity web link can be found here: http://www.pasteunity.com/WebPlayer.aspx?GameID=66
Enjoy :)
Your answer
Follow this Question
Related Questions
Rotate Method via Update ? 2 Answers
Sun(light) rotation is not working 1 Answer
How to make a cube topple?(with accuracy) 1 Answer