# Projectile Direction Unity

I want to shoot a projectile in the direction the gun is facing.

With my current script it only shoots vertically. (I can also make it go horizontally but that's it).

This is my GunController class:

public Transform guntip;
public GameObject bullet;

float fireRate = 0.5f;
float nextFire = 0f;

void Update ()
{
//playershooting
if (Input.GetAxisRaw("Fire1") > 0)
{
fireRocket();
}
}

void fireRocket()
{
if(Time.time > nextFire)
{
nextFire = Time.time + fireRate;

Instantiate(bullet, guntip.position, Quaternion.Euler(new Vector3(0, 0, 0)));
}
}


This is my ProjectileController class:

Rigidbody2D MyRB;
public float rocketSpeed;

void Awake()
{
MyRB = GetComponent<Rigidbody2D>();

MyRB.AddForce(new Vector2(0, 1) * rocketSpeed, ForceMode2D.Impulse); // explosive force, change vector 2 for horizontal shooting

}


Instantiate(bullet, guntip.position, guntip.rotation);


You need to inherit rotation of guntip to make bullets face that way.

To make bullet go towards the direction it's facing, if your 2D setup is top-down:

 MyRB.AddForce(transform.up * rocketSpeed, ForceMode2D.Impulse);


If your 2D setup is side-scroller:

 MyRB.AddForce(transform.right * rocketSpeed, ForceMode2D.Impulse);

• Thanks it is facing the right direction, but it is not moving, like before. – AJ123 Jul 6 '16 at 5:35
• @AJ123 Try increasing rocketSpeed value. – S. Tarık Çetin Jul 6 '16 at 8:42
• It faces the right way but it always goes vertical. – AJ123 Jul 6 '16 at 9:50
• Do you understand what this does? MyRB.AddForce(new Vector2(0, 1) * rocketSpeed, ForceMode2D.Impulse); Its taken from your code, so you should. Why would your projectile go in any other direction than vertical? That is how you wrote it. – jgallant Jul 6 '16 at 12:08
• I know, but how do I make it go the direction it's facing – AJ123 Jul 6 '16 at 13:44

Your Awake() method at ProjectileController class should be like:

void Awake()
{
MyRB = GetComponent<Rigidbody2D>();

Vector2 direction = GameObject.Find("Gun").GetComponenet<GunController>().guntip.position - transform.position; //Get a direction vector to fire the bullet at.

direction.Normalized(); // direction vector normalized to magnitude 1

}


And Instantiation in GunController() should be like:

void fireRocket()
{
if(Time.time > nextFire)
{
nextFire = Time.time + fireRate;

Instantiate(bullet, transform.position, Quaternion.identity);
}
}


You need a direction from GunController class's guntip. Name the gun object as "Gun" (or change in code) and need to get the gun tip position so that you can get a direction to shoot the projectile at. When we are subtracting projectile's position vector from guntip's position vector, we get a direction from projectile's current position to guntip's position which is the direction to shoot projectile. But it has some magnitude in it. So we have to make the magnitude to 1 so that it doesn't effect the force you are about to apply on it. That's why Normalized() is used on the direction so the direction vector is now unit vector with just direction in it(magnitude 1). Then AddForce() on that direction.

• that does not work. – AJ123 Jul 6 '16 at 10:46
• What the result? Have you put guntip as Gun object's child? – Sourav Paul Jul 6 '16 at 15:34