如何在Unity中使用WASD移动2D对象 [英] How to move 2D Object with WASD in Unity

查看:855
本文介绍了如何在Unity中使用WASD移动2D对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My code below only works for horizontal movement. Shouldn't the vertical movement be working too? I'm just starting out with basic 2D Unity programming:

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;
    }
}

如果我删除/注释了_currentPosX及其相关代码,则我的垂直"运动有效.但是,如果我删除/注释_currentPosY及其相关代码,那么我的水平移动将起作用.

If I remove/comment out the _currentPosX and it's related codes then my Vertical movement works. But if I remove/comment out the _currentPosY and it's related codes then my Horizontal movement works.

但是,为什么我很难让他们同时工作?我想我只是想念一些东西,但是我只是个初学者,所以我无法弄清楚.

But how come I'm having trouble getting them to work at the same time? I think I'm just missing something but I can't figure it out since I'm just a beginner at this.

感谢任何可以提供建议的人.

Thanks to whoever can give advise.

为进一步澄清...

我正在编写一个简单的2d游戏,该游戏将使玩家可以使用WASD键在4个方向上移动.

I'm coding a simple 2d game that will have the player move in 4-directions using the WASD keys.

W = move up
A = move left
S = move down
D = move right

我的主要问题是我只能使两个键只能在一个轴上工作:A和D不能用于水平方向,而W和S不能完全用于垂直方向,反之亦然.

My main problem is that I can get two of the keys working only in one axis: either A and D is working for Horizontal while W and S are not working at all for Vertical movement or vice-versa.

推荐答案

您不需要这些if语句.只需使用+=将输入附加到当前变换位置即可.

You don't need those if statements. Just use += to append the input to the current transform position.

在不使用Rigidbody的情况下移动:

Move without 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;
}

使用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);
}

如果您以后想要检测碰撞,我建议使用第二个代码并移动刚体.

I suggest using the second code and moving the Rigidbody if you want to be able to detect collison later on.

注意:

您必须分配对象才能移动到编辑器中的obj插槽.如果使用第二个代码,则将带有Rigidbody2D的对象分配给编辑器中的rb插槽.

You must assign the object to move to the obj slot in the Editor. If using the second code, assign the object with the Rigidbody2D to the rb slot in the Editor.

这篇关于如何在Unity中使用WASD移动2D对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆