- Home /
Switch Vector3 direction
Hello, how can I make this script to switch the direction of its rotation on a random amount of time?
function RotateAroundPlayer () { var SwitchDirection = 1; transform.RotateAround(Player.position, Vector3(0,SwitchDirection,0), RotateAroundSpeed * Time.smoothDeltaTime); }
Answer by aldonaletto · Sep 12, 2011 at 11:37 PM
You can change the direction of rotation inverting the speed signal. To have a random time, use Random.Range(min, max) - it generates a random value between min and max. The routine RotateAroundPlayer must be called in Update:
var timeToChange: float = 0; var minTime: float = 1; // min time to revert direction var maxTime: float = 5; // max time to revert direction private var direction: float = 1;
function RotateAroundPlayer () { if (Time.time > timeToChange){ timeToChange = Time.time + Random.Range(minTime, maxTime); direction = -direction; } transform.RotateAround(Player.position, Vector3.up, direction RotateAroundSpeed Time.deltaTime);
} It's better to use Time.deltaTime instead of Time.smoothDeltaTime - despite what its name suggests, smoothDeltaTime can produce a jittery movement in this case.