Local Multiplayer 2d Platformer with 1 keyboard (UNITY)

Viewed 29

Am struggling to make this code work, the rigidbody doesn't work with 2 inputs on same client.

I have found this solution, but since it overides gravity, they started floating and are never coming down.

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

and

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

Once I jump, they stay there, I tried Thread.Sleep but doesn't work on Unity.

I tried Coroutine and also didn't work.

I tried to give enought "wait" time before applying a negative movement on Y with my coroutine.

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

The coroutine was:

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

My goal was to give enough time before simulating gravity.

1 Answers

I forgot Coroutine worked Async, therefore I added the "gravity" inside the Async method.

Am leaving the solution here in case you ever want a Local Multiplayer 2d Game.

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