简体   繁体   English

在统一脚本中,如何围绕枢轴位置“旋转”和“旋转”

[英]In unity script, how to “rotate” and “rotate to” around pivot position

in unity editor, when i enable "Pivot", gameobject will rotate around "pivot point" position, when i enable "Center", gameobject will rotate around "center point" 在Unity编辑器中,当我启用“Pivot”时,gameobject将围绕“枢轴点”位置旋转,当我启用“Center”时,gameobject将围绕“中心点”旋转

but if i use script to rotate, it always rotate around "center point", for ex, here is my scene: 但如果我使用脚本旋转,它总是围绕“中心点”旋转,对于前者,这是我的场景:

在此输入图像描述

I use following code: 我使用以下代码:

    void Start()
    {
//        transform.rotation = Quaternion.Euler(new Vector3(0, 0, 90));
        transform.RotateAround(transform.position, new Vector3(0, 0, 1), 90);
    }

it rotate around "center point" 它围绕“中心点”旋转

在此输入图像描述

if object has collider, i can get pivot point with colider.bounds, if not, how should i do? 如果对象有对撞机,我可以用colider.bounds获得枢轴点,如果没有,我该怎么办?

and even worse, in some case, i hope rotate to like set rotate in unity editor inspector, i use following code: 更糟糕的是,在某些情况下,我希望旋转到在团结编辑器检查器中设置旋转,我使用以下代码:

transform.rotation = Quaternion.Euler(new Vector3(0, 0, 90))

I have no idea to adjust above code to rotate around pivot point 我不知道调整上面的代码来绕枢轴点旋转

update 更新

I think if i can "rotate", i can also impl "rotate to", so the key point is, how should i get "pivot point" that is used in RotateAround, to object has collider, I can get with colider.bounds with transform.position, to no collider object, how should i do? 我想如果我可以“旋转”,我也可以推动“旋转到”,所以关键点是,我应该如何获得RotateAround中使用的“枢轴点”,对象具有碰撞器,我可以使用colider.bounds使用transform.position,没有对撞机对象,我该怎么办?

The pivot point is the-same as the transform.position you're already using. 枢轴点与您已经使用的transform.position相同。 If you don't like where the pivot point is or the location the rotation is rotating against, you have two options: 如果您不喜欢枢轴点所在的位置或旋转旋转的位置,您有两种选择:

1 . 1 Create new empty GameObject. 创建新的空GameObject。 Move it to the position you want your GameObject to rotate around. 将其移动到您希望GameObject旋转的位置。 Once satisfied by that location, make this new GameObject a child of the GameObject you will be rotating around. 一旦被该位置满意,请将此新GameObject作为您将要旋转的GameObject的子项。 Use the position of this new GameObject as the pivot rotation point for the transform.RotateAround function. 使用此新GameObject的位置作为transform.RotateAround函数的轴旋转点。

Drag the empty GameObject to the customPivot slot in the script below. 将空customPivot下面脚本中的customPivot插槽中。 That should give you a new pivot point to rotate your GameObject around. 这应该会给你一个新的枢轴点来旋转你的GameObject。

public Transform customPivot;

void Update()
{
    transform.RotateAround(customPivot.position, Vector3.up, 20 * Time.deltaTime);
}

2 . 2 Open a 3D application and change the pivot point of your object, then save and re-import it into Unity. 打开3D应用程序并更改对象的轴心点,然后将其保存并重新导入Unity。

Since this is just used to rotate object around, I suggest #1 . 由于这仅用于旋转物体,我建议#1 If the problem is that the pivot point is not centered, I would suggest #2 . 如果问题是枢轴点不居中,我建议#2

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

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