Velocity based movement (2D)

So I came across an issue with my movement for my character in this game. The left control works perfectly but when use the right arrow key nothing happens. When i remove the code for the left key the right key works. When i place the left key code above the right key code neither of them work. Here is the code.` public float speed = 8f;
public float upSpeed = 5f;
public Rigidbody2D player;

// Use this for initialization
void Start () {

	player = GetComponent<Rigidbody2D> ();

}

// Update is called once per frame
void Update () {

	if (Input.GetKey (KeyCode.RightArrow)) {
		player.velocity = new Vector2 (speed, 0);
	} 
	else {
	 Input.GetKeyUp(KeyCode.RightArrow);
			player.velocity = new Vector2(0, 0);
	}

	if (Input.GetKey (KeyCode.LeftArrow)) {
		player.velocity = new Vector2 (-speed , 0);
	} 
	else {
		Input.GetKeyUp(KeyCode.LeftArrow);
		player.velocity = new Vector2(0, 0);
	}

	float upMove = Time.deltaTime * 5;

	transform.Translate(0, upMove, 0, Space.World);

}

Okay, this is a little bit of a mess right now, so first off, here’s what’s happening with your code at the moment:

First, you check (per frame) whether the right arrow key is actively held. If it is, you move right.

However, if you’re not holding right, your formatting is wrong. You’re using

else {
      Input.GetKeyUp(KeyCode.RightArrow);
}

when, logically, you would want something more like

else if(Input.GetKeyUp(KeyCode.RightArrow)) {
// ...
}

The same problem applies on the second round with the left arrow key.

However, there’s an even simpler approach to all of this in the first place. If you make use of Unity’s control systems, you can use GetAxis() (or GetAxisRaw()) to define your control scheme in fewer terms with reasonable accuracy.

To give an example of this:

void Update () {

     float inputHorizontal = Input.GetAxisRaw("Horizontal");

     player.velocity = Vector2.right * inputHorizontal * speed;
     // ...
}

In this manner, your inputs are controlled by left and right (or, for that matter, A and D in the WASD control scheme) and are theoretically customizable. Additionally, it automatically balances out the input back to 0 if you hold both to the left and right simultaneously.

Edit: That said, I would generally recommend against directly setting the velocity vector directly, and would instead suggest utilizing AddForce(), as per the typical recommendations. Doing so involves a fair bit more work, however, but can result in very clean gameplay.