问题
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