# Random smooth rotation on z-axis every other second

Hi, I am a bit of a beginner when it comes to scripting but what I’m trying to do is to get a platform in a 2D space go smoothly from the current angle to a new random angle on the z-axis and then, after a delay, repeat.

Any pointers to get me heading in the right direction will be most appreciated

``````void Start ()
{
player = GameObject.FindWithTag("Player");
}

void Update()
{
StartCoroutine(Rotation());
}

IEnumerator Rotation ()
{
yield return new WaitForSeconds(startRotation);

while (player != null)
{
Quaternion newRotation = Quaternion.Euler (0, 0, Random.Range (minRotate,
maxRotate));
transform.rotation = Quaternion.Slerp (transform.rotation, newRotation,
rotationTime);

yield return new WaitForSeconds(Random.Range(1.5f, 4f));
}
}
``````

Hii…

`````` float speed = 10f;
bool isEnabled = true;

IEnumerator IncreaseTheSpeed()
{
yield return new WaitForSeconds (UnityEngine.Random.Range(5f,10f));
speed = Random.Range(5 , 10);
if(isEnabled)
{
StartCoroutine (IncreaseTheSpeed ());
}
}`
``````

Call this once in Update. Hope this works for u…

Thanks.

Hi,

You don’t need a coroutine to do that Update will be enough. And instead of “rotationTime” use rotationSpeed (which would be a float) * Time.deltaTime. And that should do the trick ! Oh and prefer Lerp to Slerp, if you want the rotation to increase regurlarly you need the interpolation to be linear.

``````void Update () {
Quaternion newRotation = Quaternion.Euler(...);
transform.rotation = Quaternion.Lerp(transform.rotation, newRotation, rotationSpeed * Time.deltaTime);
}
``````

EDIT: Youps, i didn’t read to the end of the sentence, didn’t saw you need to repeat the rotation, coroutine will be good in this case ^^ So instead of void Update, IENumerator yourFunction () with just a little yield return new WaitForSeconds(Random.range(…)) at the end and calling the coroutine in the Update. Sorry for my mistake \$\$