# Random rotation

 0 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 more ▼ asked Apr 27 '11 at 10:06 AM Hektor 35 ● 9 ● 9 ● 17 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? Apr 27 '11 at 10:14 AM Kacer add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

 0 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(); } `````` more ▼ answered Apr 27 '11 at 10:43 AM Joshua 6.4k ● 19 ● 25 ● 70 Thank you very much for the help, the script now works perfectly! Apr 27 '11 at 02:36 PM Hektor Years of programming 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: My favorite language is Python! Apr 27 '11 at 05:19 PM flaviusxvii 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. Apr 28 '11 at 12:20 AM Joshua add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users
 0 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 :/] more ▼ answered Apr 27 '11 at 10:31 AM Cyb3rManiak 1.6k ● 1 ● 4 ● 17 I'm sorry, but the script does not work well. The variable 'choice' still change every frame. Thanks anyway for your time. Apr 27 '11 at 02:33 PM Hektor Yup, my bad. Sorry about that :/ I've edited the answer to correct it. Apr 28 '11 at 03:17 PM Cyb3rManiak add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

By Email:

Topics:

x2163
x572
x106

asked: Apr 27 '11 at 10:06 AM

Seen: 1578 times

Last Updated: Apr 27 '11 at 10:06 AM