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

编程入门 行业动态 更新时间:2024-10-25 05:26:42
本文介绍了如何在 Unity 中使用 C# 更改对象的枢轴点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我正在尝试使用 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屋!

更多推荐

[db:关键词]

本文发布于:2023-04-28 17:41:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1176909.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:枢轴   对象   如何在   Unity

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!