I am creating a top down turret defense game, and in those types of games when the turret shoots it always hits. What I am trying to do is calculate where an object will be based on the objects speed and the bullet speed so I can then rotate my turret to shoot at the correct location. If I point the turret directly at the object it will miss if the object is too far away.
My bullets/targets don’t use AdForce/Velocity, I am using transform.Translate() and transform.Rotate() to move everything, and all of the formulas I found are using velocity as part of their calculations.
I found the following on reddit and tried my best to convert it to C#, but it doesn’t work for me.
I would say that a proper tower defense game doesn’t allow this.
Either make you turrets shoot faster or make the bullet/projectile ALWAYS moving towards the first enemy in line.(Homing)
Also, consider using velocitys to move things around. It doesn’t make sense to “move” an object by changing the position manually. Let velocitys do that.
This is only true if you are using the FixedUpdate() instead of the Update(). Change to velocity if you want Update()
But to answer your question.
You know how fast the enemies are moving(units/second).
You also know the speed of the bullet to the target.
float a = (Length between tower and enemy) / bullet_speed. (prediction of how long it takes for the bullet to get to the enemy).
float b = enemy_speed * time (this is the distance the enemy walks from when the bullet are fired and when the bullet reaches the last enemy destination).
Solve C! Pythagoras theorem.
a^2 + b^2 = c^2
EDIT: This is only calculated when the enemy walks in a straight line after the bullet has been fired. So if your enemy does make a turn after the bullet has been fired, this does not work.