如何在 Unity 中使用 C# 更改对象的枢轴点? [英] How to change object's pivot point using C# in Unity?

查看:51
本文介绍了如何在 Unity 中使用 C# 更改对象的枢轴点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to change the Pivot Point of an object in Unity 3D using C#.

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

I am aware of editing the object in a design software beforehand to desired point but, in my specific situation, the Pivot Point needs to be changed while game in running.

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

Here is the code I am using to rotate the object from its left side:

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

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

I would like to use same method to apply rotation from right side. For that, I need to change the pivot point of the object while game is running.

谢谢.

推荐答案

没有办法直接改变pivots,

There is no way to change pivots directly,

解决方法,在您想要新枢轴的位置创建一个空的游戏对象,并将父变换设置为此游戏对象.

Workaround, create an empty game object(s) in position of where you want your new pivot and set the parent transform to this game object.

gameObject.transform.SetParent(pivotGameObject);

移除父级

gameObject.transform.SetParent(null);

这篇关于如何在 Unity 中使用 C# 更改对象的枢轴点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆