繁体   English   中英

带 1 个键盘的本地多人 2d 平台游戏 (UNITY)

[英]Local Multiplayer 2d Platformer with 1 keyboard (UNITY)

我正在努力使这段代码工作,刚体不能在同一个客户端上使用 2 个输入。

我找到了这个解决方案,但由于它超越了重力,它们开始漂浮并且永远不会下降。

using System.Collections.Generic;
using UnityEngine;

public class MoveArrows : MonoBehaviour
{
    public float Speed;
    float MovementY;
    float MovementX;

    Rigidbody2D rigidBody;

    // Start is called before the first frame update
    void Start()
    {
        rigidBody = GetComponent<Rigidbody2D>();

        MovementY = 0;
        MovementX = 0;
    }

    // Update is called once per frame
    void Update()
    {
        rigidBody.velocity = new Vector2(MovementX * Speed * Time.deltaTime, MovementY * Speed * Time.deltaTime);

        if (Input.GetKeyDown(KeyCode.UpArrow))
        {
            MovementY = 2;

        }

        if (Input.GetKeyDown(KeyCode.RightArrow))
        {
            MovementX = 1;
        }

        if (Input.GetKeyDown(KeyCode.LeftArrow))
        {
            MovementX = -1;
        }
    }
}

using System.Collections.Generic;
using UnityEngine;

public class MoveWASD : MonoBehaviour
{
    public float Speed;
    float MovementY;
    float MovementX;

    Rigidbody2D rigidBody;

    // Start is called before the first frame update
    void Start()
    {
        rigidBody = GetComponent<Rigidbody2D>();

        MovementY = 0;
        MovementX = 0;
    }

    // Update is called once per frame
    void Update()
    {
        rigidBody.velocity = new Vector2(MovementX * Speed * Time.deltaTime, MovementY * Speed * Time.deltaTime);

        if (Input.GetKeyDown(KeyCode.W))
        {
            MovementY = 2;

        }

        if (Input.GetKeyDown(KeyCode.D))
        {
            MovementX = 1;
        }

        if (Input.GetKeyDown(KeyCode.A))
        {
            MovementX = -1;
        }
    }
}

一旦我跳起来,他们就呆在那里,我尝试了 Thread.Sleep 但在 Unity 上不起作用。

我尝试了协程,也没有工作。

在用我的协程对 Y 应用负运动之前,我试图给足够的“等待”时间。

StartCoroutine(Wait());
MovementY= -1;

协程是:

 IEnumerator Wait()
{
       yield return new WaitForSeconds(.5f);
}

我的目标是在模拟重力之前留出足够的时间。

我忘记了 Coroutine 是异步工作的,因此我在 Async 方法中添加了“重力”。

如果您想要本地多人 2d 游戏,我将把解决方案留在这里。

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

public class MoveWASD : MonoBehaviour
{
    public float Speed;
    float MovementY;
    float MovementX;

    Rigidbody2D rigidBody;

    // Start is called before the first frame update
    void Start()
    {
        rigidBody = GetComponent<Rigidbody2D>();

        MovementY = 0;
        MovementX = 0;
    }

    // Update is called once per frame
    void Update()
    {
        rigidBody.velocity = new Vector2(MovementX * Speed * Time.deltaTime, MovementY * Speed * Time.deltaTime);

        if (Input.GetKeyDown(KeyCode.W))
        {
            MovementY = 2;
            StartCoroutine(Wait());

        }

        if (Input.GetKeyDown(KeyCode.D))
        {
            MovementX = 1;
        }

        if (Input.GetKeyDown(KeyCode.A))
        {
            MovementX = -1;
        }
    }
    IEnumerator Wait()
    {
        yield return new WaitForSeconds(.5f);
        MovementY = -1;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM