首页 > 解决方案 > Unity - 围绕 2D 中的点旋转对象

问题描述

我一直在 Unity 中开发 2D 游戏,我需要找到一种方法来围绕某个点旋转精灵。我知道对于 3D 游戏,Unity 有一个内置transform.RotateAround()功能,但我不确定如何实现 2D 等效。如果有人可以提供帮助,将不胜感激。

标签: unity3drotation2d

解决方案


您可以使用相同的功能。transform.RotateAround()需要 a Vector3 point、 aVector3 axis和 afloat angle度数。

点和角度很容易解释,但轴有点不那么清楚。这本质上是旋转方向。在默认的 Unity2D 游戏中,z 是您的深度(进入屏幕),您需要绕 Z 轴旋转:new Vector3(0,0,1)Vector3.forward.

尝试类似:

Vector3 point = new Vector3(5,0,0);
Vector3 axis = new Vector3(0,0,1);
transform.RotateAround(point, axis, Time.deltaTime * 10);

推荐阅读