# Bullet direction is affected while player moving

Vector3 mousePos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
float angle = Mathf.Atan2 (mousePos.y - transform.position.y, mousePos.x - transform.position.x);

``````			for (int i = 0; i < 5; i++) {
GameObject bu = Instantiate (bullet, transform.position, Quaternion.identity) as GameObject;

angle = Mathf.Atan2 (mousePos.y - transform.position.y, mousePos.x - transform.position.x) - Mathf.PI/2.0f+Mathf.PI*((float)i)/4.0f;
float xspeed = speed * Mathf.Cos (angle) * 1.5f;
float yspeed = speed * Mathf.Sin (angle) * 1.5f;
bu.GetComponent<Rigidbody2D> ().velocity = new Vector2 (xspeed, yspeed);
``````

This is code snippet of my code used. I tend to get 5 lines of bullets while player fires(180 degrees between 1st and 5th line, and 45 degrees for near lines). It works well when my player remains static. However, when my player moves, the angle between 1st and 5th line shrinks for some reason (my bullets gets more concentrated). I checked the velocity of bullets, it is right. How does this situation happen? Is the motion of player affect the bullets?

Thank you!

using UnityEngine;
using System.Collections;

``````public class shootBullet : MonoBehaviour
{
public GameObject projectile;
public Rigidbody attachedObj;
void Update ()
{
if (Input.GetButtonDown("Fire1"))//left control and left mouse button
{
// instantiate a prefab, at the position of the object that has this script, with the default rotation (gameobject rotation ignored)
Instantiate(projectile, transform.position, transform.rotation);
}
}

void FixedUpdate()
{
transform.position = attachedObj.position;

Vector3 newRot = new Vector3(attachedObj.velocity.x, 0f, attachedObj.velocity.z);

if (newRot != Vector3.zero)
transform.rotation = Quaternion.LookRotation(newRot);
}
}
``````

This is a code I found from one of my projects, hopefully it might help.