如何使用 C# 在 Unity 中实现 2D 自上而下播放器的优先 x,y 移动?

问题描述 投票:0回答:2

我正在尝试创建 2d 自上而下的玩家运动,就像 Pokemon 中一样。

  • 仅x,y移动,无对角线
  • 基于瓷砖的运动
  • 优先考虑最近按下的键

基于图块的移动并不难,有关该主题的教程丢失了,但我无法根据最近按下的键来梳理 x,y 移动。

例如... 按住“W”时,玩家会向上移动,如果同时按住“A”,玩家会向左移动,一旦松开“A”,玩家会继续向上。

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

public class PlayerController : MonoBehaviour
{
    public float walkSpeed = 5f;
    public float runSpeed = 10f;
    public float currentSpeed;

    public Transform movePoint;
    public LayerMask Barrier;

    public bool isRun;

    void Start()
    {
        movePoint.parent = null;
    }

    void Update()
    {
        if (Input.GetKey(KeyCode.LeftShift)){
            currentSpeed = runSpeed;
            isRun = true;
        }
        else
        {
            currentSpeed = walkSpeed;
            isRun = false;
        }


        transform.position = Vector3.MoveTowards(transform.position, movePoint.position, currentSpeed * Time.deltaTime);

        if (Vector3.Distance(transform.position, movePoint.position) <= .05f)
        {
            if (Mathf.Abs(Input.GetAxisRaw("Horizontal")) == 1f)
            {
                if (!Physics2D.OverlapCircle(movePoint.position + new Vector3(Input.GetAxisRaw("Horizontal"), 0f, 0f), .2f, Barrier))
                {
                    movePoint.position += new Vector3(Input.GetAxisRaw("Horizontal"), 0f, 0f);

                }
            }
        }
        else if (Mathf.Abs(Input.GetAxisRaw("Vertical")) == 1f)
        {
            if (!Physics2D.OverlapCircle(movePoint.position + new Vector3(0f, Input.GetAxisRaw("Vertical"), 0f), .2f, Barrier))
            {
                movePoint.position += new Vector3(0f, Input.GetAxisRaw("Vertical"), 0f);

            }
        }


    }
}


这是我一直在编辑的基础,试图让它按照我想要的方式工作。 运动的优先级仅适用于一个轴。

不确定我提供的代码是否最优,欢迎任何更好的方法来解决问题。

c# unity-game-engine 2d game-development
2个回答
0
投票

您只需在检测到水平输入时将另一个设置为零,即可用水平输入覆盖垂直输入。

这是一个精简的示例代码,您可以将其放在 2D 精灵游戏对象父级上,看看它的感觉如何:

public class PlayerController : MonoBehaviour
{

    [SerializeField] private float currentSpeed = 10f;

    [SerializeField] private KeyCode _moveUp = KeyCode.W;
    [SerializeField] private KeyCode _moveDown = KeyCode.S;
    [SerializeField] private KeyCode _moveLeft = KeyCode.A;
    [SerializeField] private KeyCode _moveRight = KeyCode.D; 

    void Update()
    {
        // reset to zero every frame
        Vector3Int  moveDirection = Vector3Int.zero;
        
        // Get the Inputs
        if (Input.GetKey(_moveUp)) moveDirection.y = 1;
        if (Input.GetKey(_moveDown)) moveDirection.y = -1;
        if (Input.GetKey(_moveLeft)) moveDirection.x = -1;
        if (Input.GetKey(_moveRight)) moveDirection.x = 1;

        // Return when no input detected
        if (moveDirection == Vector3Int.zero) return; 

        // Use Horizontal movement to overrides vertical movements.
        if (moveDirection.x != 0) moveDirection.y = 0;

        // Move the transform using moveDirection
        transform.position += (Vector3)moveDirection * Time.deltaTime * currentSpeed; 
    }
}

0
投票

使用UnityEngine;

公共类运动:MonoBehaviour { [SerializeField] 私有浮点数 currentSpeed = 10f;

[SerializeField] private KeyCode _moveUp = KeyCode.W;
[SerializeField] private KeyCode _moveDown = KeyCode.S;
[SerializeField] private KeyCode _moveLeft = KeyCode.A;
[SerializeField] private KeyCode _moveRight = KeyCode.D; 

void Update()
{
    // reset to zero every frame
    Vector3 moveDirection = Vector3.zero;
    
    // Get the Inputs
    if (Input.GetKey(_moveUp)) moveDirection += Vector3.up;
    if (Input.GetKey(_moveDown)) moveDirection += Vector3.down;
    if (Input.GetKey(_moveLeft)) moveDirection += Vector3.left;
    if (Input.GetKey(_moveRight)) moveDirection += Vector3.right;

    // Return when no input detected
    if (moveDirection == Vector3.zero) return; 

    // Normalize the direction to prevent faster diagonal movement
    moveDirection = moveDirection.normalized;

    // Move the transform using moveDirection
    transform.position += moveDirection * Time.deltaTime * currentSpeed; 
}

}

这将进行对角线移动

© www.soinside.com 2019 - 2024. All rights reserved.