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.