2d platformer player shoots

Hello everyone!

Not very good in the script, I’m learning.

I’m trying to write this script for the player to shoot in 2D platformer.
Shooting in x-axis and -x axis, right and left. the position and the position where the player looks or the right or left.

Now it does not work in the game, I tried everything before you ask a question here.

I show the script:

#pragma strict

var granataPrefab : Transform;

function Update ()
   if (Input.GetMouseButton(1))
   if(Collisions.GRENADE_AMMO > 0)
    var crate = Instantiate(granataPrefab, transform.position, Quaternion.identity);             
    crate.rigidbody2D.velocity.x = Input.GetAxis("Horizontal") * 10;               
     Collisions.GRENADE_AMMO --;
     print("YOU NOW HAVE " + Collisions.GRENADE_AMMO + " GRENADES");               

Input.GetAxis(“Horizontal”) will return you a value between -1 and 1 depending on the buttons assigned to that axis. Go into Edit → Project Settings → Input to change which keys are assigned to the input “Horizontal”. By default, I believe it is the keys “left” and “right”.

Now, You’re probably getting an error at :

crate.rigidbody2D.velocity.x = Input.GetAxis("Horizontal") * 10;  

This is because you cannot change only the x value of the velocity, you can only read it.
Instead, try something like this :

crate.rigidbody2D.velocity = new Vector2( Input.GetAxis("Horizontal") * 10, 0);

If you’re using a joystick, this means you can shoot the projectile at 50% speed if you only pull the joystick 50% of the way towards the left or right. If you always want the projectile to fire in the direction of the joystick no matter how far it’s pulled. Only get the Sign of “Input.GetAxis(“Horizontal”)”.