# How to get a bullet to fire in the direction of the gun

Hello

I am working on a 2D platformer. I have a charatcer that his arm rotates and there is a gun he is holding. I am making this game for mobile and I have the arm rotation and the gun shooting on a joystick. But when the gun fires the bullets do not go in the direction of the gun. Like when the gun is facing to the right the bullets go straight up. I Would like it for the bullets to go in the same direction as the gun. Some help would be grealty appreicated!!

Here is the shoot function I have in c#

public void Shoot ()
{
Vector3 firePointPosition = new Vector3 (firePoint.position.x, firePoint.position.y, 0f);
RaycastHit2D hit = Physics2D.Raycast (firePointPosition, firePoint.right, 100, whatToHit, 0f);

``````    Debug.DrawLine(firePointPosition, (firePointPosition)*100, Color.cyan);
if (hit.collider != null)
{
Debug.DrawLine(firePointPosition, hit.point, Color.red);
Enemy enemy = hit.collider.GetComponent<Enemy>();
if (enemy != null)
{
enemy.DamageEnemy(Damage);
//Debug.Log("We hit " + hit.collider.name + "and did " + Damage + "damage");
}
}

if (Time.time >= timeToSpawnEffect)
{
Vector3 hitPos;
Vector3 hitNormal;

if (hit.collider == null)
{
hitPos = (firePointPosition) * 30;
hitNormal = new Vector3(9999, 9999, 9999);
}
else
{
hitPos = hit.point;
hitNormal = hit.normal;
}

Effect(hitPos, hitNormal);
timeToSpawnEffect = Time.time + 1 / effectSpawnRate;
}
}
``````

It looks like you are using a Vector2 (or 3) variable to define the origin (or barrel of the gun, so to speak) of the bullets and then going to the right of that, which will always be the same, regardless of the arm’s rotation. Using transform.right might work better.
An easier solution might be to child an empty GameObject to your arm, put it where you want the bullets to come out of, and start the Raycast at the child object’s position heading in its transform.right direction.

Hope this helped!

The easiest way to do this is to put an empty gameobject on the end of the barrel of the gun.

Rotate this object in such a way that the red arrow faces in the direction you want bullets to go.

After that, reference this object in your script however you’d like.

Use (thatObject).transform.position as the origin for the raycast.

Then, use (thatObject).transform.right (shorthand for 1, 0, 0) to specify a direction/velocity to your bullet. If you do this, it will always cast a ray from the middle of your empty object to the direction the red arrow (x axis) is pointing.