C# 为什么这段代码会以如此奇怪的方式移动我的游戏对象?

C# 为什么这段代码会以如此奇怪的方式移动我的游戏对象?,c#,unity3d,C#,Unity3d,我的代码 在用于查看和移动到敏感位置的更新功能中,虽然它正确指向,但不能正确移动,我可以找出原因。默认情况下,将输入解释为本地方向和距离。您提供的是世界方向和距离的输入,因此应使用可选的第2个参数并指定空间。世界: Vector2 moveDirection; moveDirection = (Sensitive.transform.position - gameObject.transform.position).normalized; floa

我的代码

在用于查看和移动到敏感位置的更新功能中,虽然它正确指向,但不能正确移动,我可以找出原因。

默认情况下,将输入解释为本地方向和距离。您提供的是世界方向和距离的输入,因此应使用可选的第2个参数并指定空间。世界:

        Vector2 moveDirection;
        moveDirection = (Sensitive.transform.position - gameObject.transform.position).normalized;
        float deg = Mathf.Rad2Deg * Mathf.Atan(moveDirection.x / -moveDirection.y);
        if (moveDirection.y < 0) deg -= 180;
        Vector3 to = new Vector3(0, 0, deg);
        transform.eulerAngles = to;
        transform.Translate(new Vector3(moveDirection.x, moveDirection.y) * speed * Time.deltaTime);
transform.Translate(new Vector3(moveDirection.x, moveDirection.y) 
    * (speed * Time.deltaTime), Space.World);