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

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();

}

``````

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 :/]