【问题标题】:How to move 2D Object with WASD in Unity如何在 Unity 中使用 WASD 移动 2D 对象
【发布时间】:2017-10-15 22:59:51
【问题描述】:

我下面的代码仅适用于水平移动。垂直运动不应该也起作用吗?我刚刚开始使用基本的 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;
    }
}

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

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

感谢能给出建议的人。

编辑:进一步澄清...

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

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

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

【问题讨论】:

  • 你能描述一下你的控制吗? WASD 是做什么用的?
  • 只是在 2d 游戏屏幕(精灵)中上下左右移动
  • 物体是否有rigidbody2d?
  • 我稍后会在添加敌人时添加它
  • 如果您仍在努力为运动应用边界,请查看另一个用户提出的this 问题。我在那里留下了如何做到这一点的答案。

标签: c# unity3d monodevelop unity3d-2dtools


【解决方案1】:

您不需要那些 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;
}

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

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

注意

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

【讨论】:

  • Time.deltaTime 做了什么,我还能使用我的 CheckBoundary();防止玩家离开相机屏幕视图边界的功能?
  • 它确保每台设备/计算机上的移动速度相同。有些设备比其他设备更快。否则,玩家与那些拥有快速计算机的玩家对战是不公平的,因为他们的角色会移动得更快。至于你的第二个问题,是的。如果你不能这样做,那么创建一个关于将玩家移动限制在某些边界内的新问题。
  • 你不应该在 FixedUpdate 中而不是在 Update 函数中处理物理计算吗?
【解决方案2】:

此代码 100% 有效(您必须尝试一下。)

public float moveSpeed = 5;


void Start()
{
   
}


 void Update()
{

    if (Input.GetKey(KeyCode.D))
    {
        transform.position += Vector3.right * moveSpeed * Time.deltaTime;
        
    }
    else if (Input.GetKey(KeyCode.A))
    {
        transform.position += Vector3.right * -moveSpeed * Time.deltaTime;
        
    }

    else if (Input.GetKey(KeyCode.W))
    {
        transform.position += Vector3.up * moveSpeed * Time.deltaTime;

    }
    else if (Input.GetKey(KeyCode.S))
    {
        transform.position += Vector3.up * -moveSpeed * Time.deltaTime;

    }
}

【讨论】:

  • 截至 21 年 10 月为我工作
【解决方案3】:

尝试将 Vector2 函数中的值 0 更改为当前 x/y 位置...我的项目遇到了类似的问题

if (userInputV < 0) 
        _currentPosY -= new Vector2 (/*current position*/, speed); 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-30
    相关资源
    最近更新 更多