- Home /
Random rotation
Hello, in my scene there are some Transforms who must turn in a random direction when they pass beyond some areas. I made this script.
var speed : int = 5
function Update ()
{
transform.Translate(Vector3.left*speed/10);
if((transform.position.z <= 15)||(transform.position.z >= 1985)||(transform.position.x <= 15)||(transform.position.x >= 1985))
{dontFall();}
}
function dontFall()
{
var choice : float = Random.Range(100,-100);
if(choice < 0)
{leftRot();}
else
{rightRot();}
}
function leftRot()
{transform.Rotate(0,speed * 2,0);}
function rightRot()
{transform.Rotate(0,speed * -2,0);}
Do not give me error messages, but the variable choice is constantly changing and the transform turns both right and left. How can I fix it?
-H
im sorry if im being a retard, but it seems to me that the script is doing exactly what you want it to do, can you please elaborate?
Answer by Joshua · Apr 27, 2011 at 10:43 AM
Hey Hektor, I can see you're new to scripting so one little thing, you say:
{dontFall();}
When calling a function there is no need for those brackets. You can just leave them out.
Then you have leftRot and rightRot. If your object it out of bounds you constantly pick a random number and then rotate that way. You want to once pick a random number and then constantly rotate that way. Add a boolean that checks if you've done the random picking. So on top of your script put var randomised : boolean = false; and then where if now says
{
var choice : float = Random.Range(100,-100);
if(choice < 0)
{leftRot();}
else
{rightRot();}
}
It should say:
if(!randomised){ var choice : float = Random.Range(100,-100); randomised = true; if(choice < 0) leftRot(); else rightRot();
}
Thank you very much for the help, the script now works perfectly!
Years of program$$anonymous$$g have trained me to ALWAYS use brackets for things like if statement blocks. Beginners don't realize the implications of no braces and often try to add more to the conditional block forgetting that only the first statement gets executed if brackets are missing. Heed my warning and save hours of debugging!
Irony alert: $$anonymous$$y favorite language is Python!
True, fair enough. To be honest, I also nearly always force myself to use them. The way he did it looks weird to me though :p samec line 'n stuff.
Answer by Cyb3rManiak · Apr 27, 2011 at 10:31 AM
You should turn leftRot() and rightRot() into Coroutines. The idea is that right now you randomly choose the direction each frame, when what you want is when the object enters the area - decide the direction, and make a method run in that direction until you tell it to stop.
This should give you an idea of how to proceed:
var defaultSpeed : int = 5 var randomRotating: boolean = false;
function Update () {
transform.Translate(Vector3.left*speed/10);
if((transform.position.z <= 15)||(transform.position.z >= 1985)||(transform.position.x <= 15)||(transform.position.x >= 1985))
{
if (!randomRotating)
dontFall();
}
else if (randomRotating)
{
randomRotating = false;
StopCoroutine("Rotate");
}
}
function dontFall() { var choice : float = Random.Range(100,-100); if(choice < 0) { StartCoroutine("Rotate", defaultSpeed); } else { StartCoroutine("Rotate", -defaultSpeed); } }
function Rotate(float speed) { randomRotating = true;
while (randomRotating)
{
transform.Rotate(0, speed * 2, 0);
yield;
}
}
As a side note, you better check OnTriggerStay and the physics of Unity in general, since checking if your object is in the range you want every frame is not efficient at all...
[Edit: I've revised the answer in case someone ever stumbles upon it again. Stupid rookie mistake on my part. Forgetting to add a while loop with a "yield" inside Rotate()... stupid stupid stupid :/]
I'm sorry, but the script does not work well. The variable 'choice' still change every frame. Thanks anyway for your time.
Yup, my bad. Sorry about that :/ I've edited the answer to correct it.