I’m trying to make it where an object randomly rotates on its Z axis in a 2D game I’m making. Here’s the code I used.
transform.localRotation = Random.rotation;
How do I make it where it rotates only on the Z axis?
I’m trying to make it where an object randomly rotates on its Z axis in a 2D game I’m making. Here’s the code I used.
transform.localRotation = Random.rotation;
How do I make it where it rotates only on the Z axis?
Not fully sure if you mean to rotate the local, or mean to rotate the global? But either way:
transform.Rotate(0, 0, speed);
is the easy way to rotate something on just that axis. In that example speed is set within the Z of the Euler, and just set that float to however fast or slow you want it to be.
However, you mentioned it’s a 2D game, so there is no Z axis, only X and Y. So in that case it’s just (0, speed), if Y is the correct axis, might actually be the X?
But if you want to setup kinda the way you have, just declare a Vector2
that’s called randomVector
or whatever, then use a random number in the appropriate axis:
Vector2 randomVector;
float random = Random.Range(1, 10);
randomVector = new Vector2(0, random);
basically, but that would only set the new rotation, not rotate it. To do it that way you would need to Lerp and possibly use Quaternions.