C# unity 2d中的方向实体移动故障

C# unity 2d中的方向实体移动故障,c#,unity3d,direction,C#,Unity3d,Direction,我正在Unity 2D中创建一个游戏。我有龙,我正在添加到场景中。龙只能向四个方向中的一个移动,上、下、左、右。左右移动的龙完全按照预期移动。然而,上下移动的龙有一个问题,它们以一个角度移动。所有向上移动的龙都以45度角向上和向右移动。所有向下移动的龙都以45度角向下和向左移动。 起初,我认为动画师将龙移动到其他位置是个问题,但我从预制件中移除了动画师组件,问题仍然存在 下面是我用来移动龙的代码 using System.Collections; using System.Collections

我正在Unity 2D中创建一个游戏。我有龙,我正在添加到场景中。龙只能向四个方向中的一个移动,上、下、左、右。左右移动的龙完全按照预期移动。然而,上下移动的龙有一个问题,它们以一个角度移动。所有向上移动的龙都以45度角向上和向右移动。所有向下移动的龙都以45度角向下和向左移动。 起初,我认为动画师将龙移动到其他位置是个问题,但我从预制件中移除了动画师组件,问题仍然存在

下面是我用来移动龙的代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DragonMovment : MonoBehaviour {
    public string Direction;    //needs to be set in the prefab

    public float DragonSpeed;   //speed of dragon

    Rigidbody2D rb; 

    public Transform Boundries;

    // Use this for initialization
    void Start ()
    {
        rb = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void FixedUpdate ()
    {
        float MoveRight = 1;
        float MoveLeft = -1;
        float MoveUp = 1;
        float MoveDown = -1;

        if (Direction== "R")
        {
            rb.velocity = new Vector3(DragonSpeed * MoveRight, rb.velocity.y);
        }
        if (Direction == "L")
        {
            rb.velocity = new Vector3(DragonSpeed * MoveLeft, rb.velocity.y);
        }
        if (Direction == "U")
        {
            rb.velocity = new Vector3(DragonSpeed * MoveUp, rb.velocity.x);
        }
        if (Direction == "D")
        {
            rb.velocity = new Vector3(DragonSpeed * MoveDown, rb.velocity.x);
        }


    }
}
编辑。 那么,为什么下面的方法会起作用呢

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerControler : MonoBehaviour {
    // speed of movment 
    public float Speed;
    // rb
    Rigidbody2D rb;


    public Transform Boundries;

    // Use this for initialization
    void Start () {
        rb = GetComponent<Rigidbody2D>();
    }

    void FixedUpdate()
    {
        // get horizontal input
        float MoveSide = Input.GetAxis("Horizontal");
        //get vertical input
        float MoveVert = Input.GetAxis("Vertical");
        // move horizontal
        rb.velocity = new Vector3(Speed * MoveVert, rb.velocity.y);
        // move vertical 
        rb.velocity = new Vector3(Speed * MoveSide, rb.velocity.x);
    }
}

但是另一个代码没有发送?

你得到了向量3的y方向的速度x

if (Direction == "U")
{
    rb.velocity = new Vector3(rb.velocity.x, DragonSpeed * MoveUp);
}
if (Direction == "D")
{
    rb.velocity = new Vector3(rb.velocity.x, DragonSpeed * MoveDown);
}
当您重写后续语句中的值时,它在您的播放器脚本中工作

    float MoveSide = Input.GetAxis("Horizontal"); //eg 1
    float MoveVert = Input.GetAxis("Vertical"); // eg 1

    // setting your x velocity incorrectly to the y (vert) velocity speed and keeping y the same velocity as start of frame
    rb.velocity = new Vector3(Speed * MoveVert, rb.velocity.y);
    // Set the y to the x value of the statement above so it is now in the correct vector and set the x to the correct hoz velocity
    rb.velocity = new Vector3(Speed * MoveSide, rb.velocity.x);

// effectively doing 
rb.velocity = new Vector3(Speed * MoveSide, Speed * MoveVert);
您还应该使用MovePosition,因为它不会直接影响物理引擎。使用velocity可以对碰撞和触发器产生连锁反应,并创建意外的物理。你的游戏对象必须被标记为运动学,否则下面的信息会导致它们立即传送到新的位置

var movementDirection = new Vector3(Speed * MoveSide, Speed * MoveVert);
rb.MovePosition(transform.position + movementDirection * Time.deltaTime);
并且*Time.deltaTime可确保不同帧率下的移动一致。如果在每秒30帧的机器上运行游戏,游戏对象的移动速度将低于每秒60帧。Time.deltaTime计算自上一帧以来经过的物理时间,并确保无论帧速率如何,经过的距离都是相同的

e、 g表示游戏对象每帧更新移动1次。在每秒30帧的机器上运行一秒钟后,该对象将移动30帧。在每秒60帧的机器上运行一秒钟后,对象将移动60帧

Time.deltaTime=.2s on 30 fps so 1 movement * .2 = move .2 per frame * 30 frames in the second = 60 moved
Time.deltaTime=.1s on 60 fps so 1 movement * .1 = move .1 per frame * 60 frames in the second = 60 moved

好的,谢谢。但是,我的角色移动器工作对我来说没有意义,因为我使用相同的代码格式将角色移动器代码添加到原始问题中,以便您可以看到它。“你能给我解释一下吗,这样我以后才能明白?”亚历克斯在我的回答中补充道