How to change object's pivot point using C# in Unity?

做~自己de王妃 提交于 2021-01-29 16:11:42

问题


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.

Thank you.


回答1:


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

to remove parent

gameObject.transform.SetParent(null);


来源:https://stackoverflow.com/questions/61990806/how-to-change-objects-pivot-point-using-c-sharp-in-unity

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!