I am making a Ping Pong test game where two people can play (arrowDown + arrowUP and w + s) but i can’t get it to work. I made this by “translating” it from another script in UnityScript.
How can I get the player object to move up or down?
[RequireComponent(typeof(Rigidbody2D))]
[RequireComponent(typeof(BoxCollider2D))]
public class PlayerControl : MonoBehaviour {
//Var for which key to press to move
public KeyCode moveUp;
public KeyCode moveDown;
//The speed of the player
public int speed = 12;
// Update is called once per frame
void Update () {
if (Input.GetKey(moveUp))
{
rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.y, speed);
}
else if (Input.GetKey(moveDown))
{
rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x, speed *-1);
}
else
{
rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.y, 0);
}
rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x, 0);
}
}