# 8-direction fire with left joystick

I’m working on a 2d platformer based on the Unity 2D platformer tutorial. My game is gamepad(360) only. I would like for the player to be able to shoot up, up/right, etc. Using the left joystick. Right now, the player can only shoot left or right based on the way he’s facing.

He is a illustration of what I’m trying to achieve.

I tried getting a “angle” based on the controller input to no avail. Here’s my code. Any help or pointing in the right direction would be appreciated.

public class Fire : MonoBehaviour
{

``````public Rigidbody2D rocket;				// Prefab of the rocket.
public float speed;						// The speed the rocket will fire at.

public float fireRate;					// Fire rate
private float nextFire;					// How soon the player can fire again

private PlayerController playerCtrl;	// Reference to the PlayerController script.
private Animator anim;					// Reference to the Animator component.

float PosH;   //value of change in the Horizontal axis of the controller
float PosV;   //value of change in the Vertical axis of the controller

void Awake()
{
// Setting up the references.
anim = transform.root.gameObject.GetComponent<Animator>();
playerCtrl = transform.root.GetComponent<PlayerController>();
}

void Update ()
{
PosH = (Input.GetAxis("Horizontal"));   //get the float of change of the axis
PosV = (Input.GetAxis("Vertical"));     //get the float of change of the axis

var angle = Mathf.Atan2(PosH, PosV) * Mathf.Rad2Deg;
Vector3 rotationVector = new Vector3 (0, 0, angle);
transform.rotation = Quaternion.Euler(rotationVector);

// If the fire button is pressed...
if(Input.GetButtonDown("Fire1") && Time.time > nextFire)
{
// Set up nextFire
nextFire = Time.time + fireRate;

// ... set the animator Shoot trigger parameter
anim.SetTrigger("shoot");
//audio.Play();

// If the player is facing right...
if(playerCtrl.facingRight)
{
// ... instantiate the rocket facing right and set it's velocity to the right.
Rigidbody2D bulletInstance = Instantiate(rocket, transform.position, Quaternion.Euler(rotationVector)) as Rigidbody2D;
//Rigidbody2D bulletInstance = Instantiate(rocket, transform.position, Quaternion.Euler(new Vector3(0,0,0))) as Rigidbody2D;
bulletInstance.velocity = new Vector2(speed, 0);
}
else
{
// Otherwise instantiate the rocket facing left and set it's velocity to the left.
Rigidbody2D bulletInstance = Instantiate(rocket, transform.position, Quaternion.Euler(rotationVector)) as Rigidbody2D;
//Rigidbody2D bulletInstance = Instantiate(rocket, transform.position, Quaternion.Euler(new Vector3(0,0,0))) as Rigidbody2D;
bulletInstance.velocity = new Vector2(-speed, 0);
}
}
}
``````

}

I believe what you have here is close to working. It is not how I would do it, so I cannot be sure. Try making this change to lines 50 and 57:

`````` bulletInstance.velocity = rotationVector.normalized * speed;
``````

Alternately you could do:

`````` bulletInstance.velocity = bulletInstance.transform.right* speed;
``````

I wasn’t sure from your question if you needed to round your angle to one of the 8 directions or not.