Rigidbody2d Moving up and Down but not left and right

Hello guys,

Fresh unity user here,

My RigidBody2D (pong game) is correctly moving up and down, but not left and right. The code seems simple, but i can’t understand why it’s not moving.

I have set the controls to the correct KeyCode.

Can you explain what mistake i have made?

There is my code :

var moveUp : KeyCode; 
var moveDown : KeyCode;
var moveLeft : KeyCode;
var moveRight : KeyCode;

var speed : float = 12;

function Update () {

	if (Input.GetKey(moveUp))
	{
		GetComponent.<Rigidbody2D>().velocity.y = speed;
	}
	else if (Input.GetKey(moveDown))
	{
		GetComponent.<Rigidbody2D>().velocity.y = -speed;
	}
	else if (Input.GetKey(moveLeft))
	{
		GetComponent.<Rigidbody2D>().velocity.x = -speed;
	}
	else if (Input.GetKey(moveRight))
	{
		GetComponent.<Rigidbody2D>().velocity.x = speed;
	}
	else
	{
		GetComponent.<Rigidbody2D>().velocity.y = 0;
	}

	GetComponent.<Rigidbody2D>().velocity.x = 0;

}

Thanks a lot.

You are resetting x velocity to 0 in line 31. So, you set speed correctly, but then you set it to 0 in the end.

Something like this would be better:

function Update ()
{
 
     GetComponent.<Rigidbody2D>().velocity.y = 0;
     GetComponent.<Rigidbody2D>().velocity.x = 0;

     if (Input.GetKey(moveUp))
         GetComponent.<Rigidbody2D>().velocity.y = speed;

     if (Input.GetKey(moveDown))
         GetComponent.<Rigidbody2D>().velocity.y = -speed;

     if (Input.GetKey(moveLeft))
         GetComponent.<Rigidbody2D>().velocity.x = -speed;

     if (Input.GetKey(moveRight))
         GetComponent.<Rigidbody2D>().velocity.x = speed;

 }