Delay in Vector2 update

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.

I assume that fireVector means the velocity at which the projectile should travel when fired from the space ship. In this case, it looks like you are setting it to the correct direction, but with a length of 1. Is there any other speed applied? What you probably want is to take the velocity of the space ship as a baseline and add the projectile’s speed on top of that, so you get a final velocity into the direction of the player that has a higher speed than the ship.

So, what I would do is:

  • Get direction to player and velocity of ship (either use Rigidbody or track it yourself)
  • Spawn the projectile and set its rigidbody velocity to the direction to the player and scale this vector by the speed of the ship + some speed value.

I figured it out. Because I was Instantiating and then changing the fireVector the projectile GameObject would use the players position at the time the last shot was fired.
Just being a dumb dumb. Note to self, if you don’t gave a reference to the instantiated object make all changes before instantiating.