如何在 Unity 中使用 WASD 移动 2D 对象

512*_*bee 1 c# monodevelop unity-game-engine unity3d-2dtools

我下面的代码仅适用于水平移动。垂直运动不应该也起作用吗?我刚刚开始学习基本的 2D Unity 编程:

public class Player : MonoBehaviour {

    //These fields will be exposed to Unity so the dev can set the parameters there
    [SerializeField] private float speed = 1f;
    [SerializeField] private float upY;
    [SerializeField] private float downY;
    [SerializeField] private float leftX;
    [SerializeField] private float rightX;

    private Transform _transformY;
    private Transform _transformX;
    private Vector2 _currentPosY;
    private Vector2 _currentPosX;

    // Use this for initialization
    void Start () {
        _transformY = gameObject.GetComponent<Transform> ();
        _currentPosY = _transformY.position;        

        _transformX = gameObject.GetComponent<Transform> ();
        _currentPosX = _transformX.position;
    }

    // Update is called once per frame
    void Update () {
        _currentPosY = _transformY.position;
        _currentPosX = _transformX.position;

        float userInputV = Input.GetAxis ("Vertical");
        float userInputH = Input.GetAxis ("Horizontal");

        if (userInputV < 0) 
            _currentPosY -= new Vector2 (0, speed);     

        if (userInputV > 0)
            _currentPosY += new Vector2 (0, speed);

        if (userInputH < 0)
            _currentPosX -= new Vector2 (speed, 0);

        if (userInputH > 0)
            _currentPosX += new Vector2 (speed, 0);

        CheckBoundary ();

        _transformY.position = _currentPosY;
        _transformX.position = _currentPosX;
    }

    private void CheckBoundary(){
        if (_currentPosY.y < upY)
            _currentPosY.y = upY;

        if (_currentPosY.y > downY)
            _currentPosY.y = downY;

        if (_currentPosX.x < leftX)
            _currentPosX.x = leftX;

        if (_currentPosX.x > rightX)
            _currentPosX.x = rightX;
    }
}
Run Code Online (Sandbox Code Playgroud)

如果我删除/注释掉_currentPosX和它相关的代码,那么我的垂直移动就起作用了。但是如果我删除/注释掉_currentPosY和它相关的代码,那么我的水平移动就会起作用。

但是为什么我无法让它们同时工作呢?我想我只是错过了一些东西,但我无法弄清楚,因为我只是这方面的初学者。

感谢哪位能给点建议。

编辑:为了进一步澄清......

我正在编写一个简单的 2d 游戏,让玩家使用 WASD 键在 4 个方向上移动。

W = move up
A = move left
S = move down
D = move right
Run Code Online (Sandbox Code Playgroud)

我的主要问题是我只能让两个键在一个轴上工作:A 和 D 用于水平移动,而 W 和 S 根本不适用于垂直移动,反之亦然。

Pro*_*mer 8

你不需要这些if陈述。仅用于+=将输入附加到当前变换位置。

移动没有Rigidbody

public float speed = 100;
public Transform obj;

public void Update()
{
    float h = Input.GetAxis("Horizontal");
    float v = Input.GetAxis("Vertical");

    Vector3 tempVect = new Vector3(h, v, 0);
    tempVect = tempVect.normalized * speed * Time.deltaTime;

    obj.transform.position += tempVect;
}
Run Code Online (Sandbox Code Playgroud)

移动对象Rigidbody2D

public float speed = 100;
public Rigidbody2D rb;

public void Update()
{
    float h = Input.GetAxis("Horizontal");
    float v = Input.GetAxis("Vertical");

    Vector3 tempVect = new Vector3(h, v, 0);
    tempVect = tempVect.normalized * speed * Time.deltaTime;
    rb.MovePosition(rb.transform.position + tempVect);
}
Run Code Online (Sandbox Code Playgroud)

如果您希望稍后能够检测到碰撞,我建议使用第二个代码并移动 Rigidbody。

注意

您必须指定要移动到obj编辑器中的插槽的对象。如果使用第二个代码,请将带有 的对象分配给编辑器中Rigidbody2Drb插槽。

  • 难道你不应该在FixedUpdate而不是Update函数中处理物理计算吗? (2认同)