首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我想改进*输入系统键盘*战斗机游戏2玩家

我想改进*输入系统键盘*战斗机游戏2玩家
EN

Stack Overflow用户
提问于 2022-05-12 17:03:47
回答 1查看 57关注 0票数 0

你有更好的主意来改进输入系统吗?

我对事件做了这样的输入系统,但我认为有很多输入系统,这不是个好主意:

代码语言:javascript
复制
public class InputSystemKeyboard : MonoBehaviour
{
    public event Action Left_P1 = delegate { };
    public event Action Right_P1 = delegate { };
    public event Action Jump_P1 = delegate { };

    public event Action Left_P2 = delegate { };
    public event Action Right_P2 = delegate { };
    public event Action Jump_P2 = delegate { };

    public event Action Stop_P = delegate { };


    void Update()
    {
        if (Input.GetKeyDown(KeyCode.A))
        {
            Left_P1();
        }
        if (Input.GetKeyDown(KeyCode.D))
        {
            Right_P1();
        }
        if (Input.GetKeyDown(KeyCode.W))
        {
            Jump_P1();
        }

        if (Input.GetKeyDown(KeyCode.LeftArrow))
        {
            Left_P2();
        }
        if (Input.GetKeyDown(KeyCode.RightArrow))
        {
            Right_P2();
        }
        if (Input.GetKeyDown(KeyCode.UpArrow))
        {
            Jump_P2();
        }

        if (!Input.anyKey)
        {
            Stop_P();
        }
    }
}

然后,在运动系统中,你要做的是:

代码语言:javascript
复制
public class MoveSystem : MonoBehaviour
{
    [SerializeField] private Rigidbody2D rb;
    [SerializeField] private float speed, jumpForce;

    private InputSystemKeyboard inputSystem;

    private bool moveLeft, moveRight;

     private void OnEnable()
     {
        inputSystem = GetComponent<InputSystemKeyboard>();

        inputSystem.Left_P1 += MoveLeft;
        inputSystem.Right_P1 += MoveRight;
        inputSystem.Jump_P1 += Jump;
        inputSystem.Stop_P += StopMoving;
    }
    private void OnDisable()
    {
        inputSystem.Left_P1 -= MoveLeft;
        inputSystem.Right_P1 -= MoveRight;
        inputSystem.Jump_P1 -= Jump;
        inputSystem.Stop_P -= StopMoving;
    }

    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        moveLeft = false;
        moveRight = false;  
    }

    public void MoveLeft()
    {
        moveLeft = true;
    }
    public void MoveRight()
    {
        moveRight = true;
    }
    public void Jump()
    {
        if (rb.velocity.y == 0)
        {
            rb.AddForce(Vector2.up * jumpForce);
        }
    }
    public void StopMoving()
    {
        moveLeft = false;
        moveRight = false;
        rb.velocity = Vector2.zero;
    }
    private void Update()
    {
        if (moveLeft )
        {
            rb.velocity = new Vector2(-speed, 0f);
        }
        if (moveRight)
        {
            rb.velocity = new Vector2(speed, 0f);
        }
    }
}

我想简化InputSystem并尝试改变这个结构。如果您有任何改进的想法,将不胜感激。

EN

回答 1

Stack Overflow用户

发布于 2022-05-12 17:40:02

对于水平和垂直移动,不需要检查每个键,如WASD和箭头键。如果转到编辑< Project < Input 并展开axes,您可以看到水平垂直的输入默认设置为WASD和箭头键,如下图所示。

Wup arrow用于跳转时,清除垂直部分中的负数按钮。

同时,在创建本地多人游戏时,右键单击它们并复制水平垂直部分,并分别将它们命名为Horizontal1Vertical1。如果水平被复制,则从其中删除AD键,并从重复的Horizontal1中移除箭头键,反之亦然。垂直部分也是如此。

现在,使用WASD箭头键的播放器的所有操作都简化为Input.GetAxis("Horizontal")Input.GetAxis("Vertical")

对于leftright运动,您只需写:

代码语言:javascript
复制
rb.velocity = new Vector2(speed * Input.GetAxis("Horizontal"));

Input.GetAxis("Horizontal")在按正按钮时返回1,在按负按钮时返回-1

对于跳转命令,只需编写:

代码语言:javascript
复制
if(rb.velocity.y == 0) {
    rb.AddForce(Vector2.up * jumpForce * Input.GetAxis("Vertical"));
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72219580

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档