Hello, I’ve got a problem that apparently several people have already solved, but the solutions don’t work in my case.
I’ve got a turret enemy that shoots at the player if the player is in range. The turret has a defined fire delay and reload time, but for some reason sometimes chooses not to shoot as fast as it can.
The turret shoots faster when the player is close, and slows down when the player moves further. My first thought was that the aim vector isn’t normalized (my function header is void Shoot(Vector2 aim)), but I’ve taken all steps to ensure it is and it still happens. Here’s my code for the shoot function.
public void Shoot(Vector2 aim)
{
if (currentClip > 0 && Time.time > timeOfNextShot)
{
currentClip--;
// Calculate firing angle from given aim vector, instantiate the projectile and apply force to it
float firingAngle = Vector2.SignedAngle(transform.right, aim.normalized);
GameObject projectile = Instantiate(projectilePrefab, firingPoint.transform.position, Quaternion.Euler(0f, 0f, firingAngle));
projectile.GetComponent<Rigidbody2D>().velocity = aim.normalized * projectileSpeed;
if (currentClip > 0)
{
timeOfNextShot = Time.time + fireDelay;
}
else
{
timeOfNextShot = Time.time + reloadTime;
currentClip = clipSize;
}
}
}
Does anyone have an idea as to why this happens and how I can fix it? If it helps, EnemyTurretController.cs uses raycasting to determine if the player is visible.