- Home /
Random rotation along the y axis
Hi! I have a boat that I'm trying to get to move around my map randomly. I have two coroutines. One for movement and one for rotation. Both are called as soon as the game starts. The movement code is:
IEnumerator Move() {
yield return new WaitForSeconds (0.05f);
transform.position += Vector3.forward * Time.deltaTime * thrust;
StartCoroutine (Move ());
}
The rotation one is exactly the same except the wait time is longer and the code I'm using is:
Vector3 euler = transform.eulerAngles;
euler.z = Random.Range(0f, 360f);
transform.eulerAngles = euler;
However this of course makes the ship instantly rotate but I've yet to find a way to make the rotation gradual while still keeping it random. That's the first issue. Secondly, when the ship does rotate it doesn't change movement direction. That is, if the ship is heading, say north and the random rotation flips it around, it will continue heading north, even if the bow is facing the opposite direction. I thought the code I have would account for that but apparently somethings wrong. I'm really just focused on the first problem and I think eventually I could figure out the second, but I figured if I'm posting one, why not post the other?
Thanks!
Answer by allenallenallen · Aug 10, 2015 at 04:26 AM
Use this to slowly rotate:
http://docs.unity3d.com/ScriptReference/Quaternion.Lerp.html
Ah see I found that earlier but you have to use a transform from another object right? And I don't wunna muck up the scene by adding cubes just to get their transform I was hoping it could be contained in just the boat.
You can. Just declare yourself a few eulerAngles within the script. Remember that you can make rotations simply by declaring a Vector3. You don't need another object to do this.
http://docs.unity3d.com/ScriptReference/Quaternion-eulerAngles.html
Your answer
![](https://koobas.hobune.stream/wayback/20220611235130im_/https://answers.unity.com/themes/thub/images/avi.jpg)