Unity 2D 移动和旋转问题

Unity 2D move and rotate issue

我正在为我的 2d 游戏添加一架直升机,我需要它在沿 x 轴移动的同时做圆周运动。您可以在下面找到我正在使用的代码,它使用了数学圆方程。

angle += speed * Time.deltaTime; //if you want to switch direction, use -= instead of +=
float x = startPoint.x + Mathf.Cos(angle) * radius;
float y = startPoint.y + Mathf.Sin(angle) * radius;
transform.position = new Vector2(x + 2, y);

直升机旋转正确,但我不知道如何让它沿 x 轴移动。它应该如何工作的概念图如下:

1) 制作一个空的游戏对象

2) 将你的盒子作为空游戏对象的父级

3) 围绕空游戏对象旋转盒子

4) 将空的游戏对象移到一边

如果你想避免添加一个空的父级,你可以单独跟踪旋转中心,围绕它旋转,并随时间移动它。

public class hello_rotate : MonoBehaviour
{
    float angle = 0;
    float radius = 1;
    float speed = 10;
    float linear_speed = 1;
    Vector2 centerOfRotation;
    // Start is called before the first frame update
    void Start()
    {
        centerOfRotation = transform.position;
    }

    // Update is called once per frame
    void Update()
    {
        centerOfRotation.x = centerOfRotation.x + linear_speed * Time.deltaTime;

        angle += speed * Time.deltaTime; //if you want to switch direction, use -= instead of +=
        float x = centerOfRotation.x + Mathf.Cos(angle) * radius;
        float y = centerOfRotation.y + Mathf.Sin(angle) * radius;
        transform.position = new Vector2(x + 2, y);
    }
}