Keith Power
Keith Power

Reputation: 14141

Unity rotate around z axis

I am trying to randomly rotate objects as I instantiate them. However I am having difficulty only rotating on the z axis.

GameObject currentcicrle = (GameObject)Instantiate (circlePointPrefab);//instaiate object
currentcicrle.transform.parent = parent;
currentcicrle.GetComponent<Renderer>().material = currentLineRender.GetComponent<LineRendererAttributes> ().material;
currentcicrle.transform.position = position;

currentcicrle.transform.rotation = Random.rotation;

Upvotes: 1

Views: 9758

Answers (3)

CosmicGiant
CosmicGiant

Reputation: 6441

There are several ways to achieve this. Eric's and Kjell's answers already contain some, which I'll include here for the sake of convenience (go upvote them if you like the methods), but will also extend a LOT further.


Set through (local) euler:

Direct but verbose:

transform.localEulerAngles = new Vector3(transform.localEulerAngles.x, transform.localEulerAngles.y, Random.Range(0f,360f));

Indirect through 'copy, set, replace':

var angles = transform.localEulerAngles;
angles.z = Random.Range(0f,360f);
transform.localEulerAngles = angles;

Direct and short, through extension method:

Somewhere in an extensions library:

public static class Vector3Extensions {
    public static Vector3 WithZ(this Vector3 vector, float z) {
        vector.z = z;
        return vector;
    }
}

Then, whenever you need to set just the Z of a Vector3, use the extension method. In this case:

transform.localEulerAngles = transform.localEulerAngles.WithZ(Random.Range(0f,360f));

Further extension to the transform itself:

Somewhere in an extensions library:

public static void SetLocalEulerAnglesZ(this Transform transform, float z) {
    transform.localEulerAngles = transform.localEulerAngles.WithZ(z);
}

Then setting Z can be simplified to:

transform.SetLocalEulerAnglesZ(Random.Range(0f, 360f));

Apply a rotation over the current rotation:

Rotate or RotateAround:

Useful for simple axis-aligned rotations. Defaults to local space (relative to the transform), but a fourth, Space.World parameter can be used to rotate around world axis.

transform.Rotate(0, 0, Random.Range(0f, 360f));

Useful for more complex rotations, not aligned to local or world axis.

transform.RotateAround(transform.position, transform.forward, Random.Range(0,360));

Current rotation + Euler Z rotation:

Useful for complex composite rotations. Not the case here, but in case the rotation to be applied was the product of a series of sub-rotations being precalculated before being applied. Quaternions are added onto eachother by multiplication.

transform.rotation = transform.rotation * Quaternion.Euler(0, 0, Random.Range(0f, 360f));

Upvotes: 2

Eric Warburton
Eric Warburton

Reputation: 329

currentcircle.transform.rotation = Quaternion.Euler(0.0, 0.0, Random.Range(0.0, 360.0);

Or

currentcircle.Rotate(0.0, 0.0, Random.Range(0.0, 360.0));

See Previous Answer

Upvotes: 6

Kjell Schwaricke
Kjell Schwaricke

Reputation: 129

        currentcicrle.transform.RotateAround(center.position, Vector3.forward, Random.Range(0,360); 

Upvotes: 2

Related Questions