I’m currently working on learning Unity and doing a small space shooter type project. I want the enemy ships to fire bullets at the player so I made the following code:
public void fireAtPlayer()
{
fireTimer += Time.deltaTime;
if (fireTimer > projectile.GetComponent<BulletController>().fireRate)
{
Vector2 vectorToPlayer = (PlayerController.Player.transform.position -this.transform.position).normalized;
fireTimer = 0;
Instantiate(projectile, this.transform.position, Quaternion.identity);
projectile.GetComponent<BulletController>().fireVector = vectorToPlayer;
}
}
Sorry for the sloppy formatting I can’t seem to figure out how to format it properly in this tiny box.
And for the most part everything works fine, but there seems to be a delay between when I move the player and when the bullets get my new position so they lagg behind for a second and the slower I make the fireRate the longer it takes. From what I can tell it only gets the new position after it fires a bullet at the old one.
Player is a public static PlayerController object that is also a singleton.