Unity - 围绕二维点旋转对象

Unity - Rotate an object around a point in 2D

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

您可以使用相同的功能。 transform.RotateAround() 需要 Vector3 pointVector3 axisfloat 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);

如果您正在努力理解这些转换函数,可以使用另一种方法。只需在您希望旋转的位置创建一个新游戏object。然后将精灵设为该游戏 object 的 child。当您旋转游戏 object 时,精灵应该围绕该点移动。

实际上你可以使用一些游戏对象作为锚:

    public GameObject anchor;
    public float velocidad;
    void Start () {
        velocidad = 50f;
    }

    // Update is called once per frame
    void FixedUpdate () {

        transform.RotateAround(anchor.transform.localPosition, Vector3.back, Time.deltaTime*velocidad);
    }