如何在 Unity 中使用 C# 更改对象的枢轴点?

sc1*_*c13 1 c# unity-game-engine

我正在尝试使用 C# 更改 Unity 3D 中对象的枢轴点。

我知道事先在设计软件中将对象编辑到所需的点,但在我的特定情况下,需要在游戏运行时更改枢轴点。

这是我用来从左侧旋转对象的代码:

    verticalInputLeft = Input.GetAxis("VerticalLeft");       

    rotationLeftX += verticalInputLeft * verticalSensitivity * Time.deltaTime;
    rotationLeftX = Mathf.Clamp(rotationLeftX, minAngle, maxAngle);

    transform.localEulerAngles = new Vector3(rotationLeftX, transform.localEulerAngles.y, transform.localEulerAngles.z);
Run Code Online (Sandbox Code Playgroud)

我想使用相同的方法从右侧应用旋转。为此,我需要在游戏运行时更改对象的枢轴点。

谢谢。

小智 5

其实,有一种方法可以改变枢轴。

如果直接使用 RectTransform 组件,则可以更改枢轴的值以及与屏幕位置相关的其他属性。

例子:

this.gameObject.GetComponent<RectTransform>().pivot = new Vector2(0.5f, 0.5f);
//Center the pivot
Run Code Online (Sandbox Code Playgroud)