简体   繁体   English

unity绕z轴旋转

[英]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. 但是我很难仅在z轴上旋转。

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;
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 查看上一个答案

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

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. Eric和Kjell的答案已经包含一些答案,为了方便起见,我将在此处包括在内(如果您喜欢这些方法,请对其进行投票),但还将进一步扩展。


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. 然后,每当您只需要设置Vector3的Z时,都可以使用扩展方法。 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: 然后可以将设置Z简化为:

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. 默认为局部空间(相对于变换),但是第四个Space.World参数可用于绕世界轴旋转。

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: 当前旋转+ Euler Z旋转:

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));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM