I have this weird problem that when my enemies spawn their bullets the bullets spin. The script is on an empty gameobject which spawns the bullets and it looks at the player exactly how it’s supposed to but when I make the bullet that position it just spins.
I tried commenting out the addforce part and that resulted in the bullet being spawned at eulerAngles: 0,0,0. It should be noted that this script is on a moving/flying enemy.
public GameObject target;
public GameObject bulletSpawn;
public GameObject shot;
public int force;
public float attackTimer;
void Start () {
}
void Update () {
Vector2 v_diff = (target.transform.position - bulletSpawn.transform.position);
float atan2 = Mathf.Atan2(v_diff.y, v_diff.x);
bulletSpawn.transform.rotation = Quaternion.Euler(0f, 0f, atan2 * Mathf.Rad2Deg);
float distance = Vector3.Distance(target.transform.position, transform.position);
Vector3 dir = (target.transform.position - transform.position).normalized;
float direction = Vector3.Dot(dir, transform.forward);
//Debug.Log(direction);
if (attackTimer > 0)
attackTimer -= Time.deltaTime;
if (attackTimer < 0)
attackTimer = 0;
if (distance < 7f && attackTimer == 0)
{
Shoot();
}
}
void Shoot()
{
GameObject bullet = Instantiate(shot, bulletSpawn.transform.position, bulletSpawn.transform.rotation) as GameObject;
Vector2 direction = bullet.rigidbody2D.transform.position - target.transform.position;
bullet.rigidbody2D.AddForceAtPosition(direction.normalized *- force, transform.position);
print("sas");
bullet.transform.eulerAngles = transform.eulerAngles;
Destroy(bullet, 8);
attackTimer = 2;
}
}