I hava a Tower with the position Vector2 that shoots a target when i comes close enough. The projectile should inherit the tower’s position as it’s startposition and it then gets a target Vector2 that is suppose to hit.
How do I calulate this as i need to calulate the new position in every update when the projectile knows its own position and target position plus speed?
If you have target and origin, then just doing target – origin gives you a new Vector2 which is the direction. You then need to normalize this direction vector, and multiply it by the projectile speed.
Pseudocode:
Then just use direction and multiply it by the time between frames, and add this to the bullet position.
Edit: to follow up on George’s comment
The pseudocode I showed will always miss the target if the target is moving since it assumes the bullet cannot curve and does not take into account target velocity. If the bullet curves, as George said, you need to recalculate the direction vector each frame.
To have a non curving bullet but that takes into account the target’s velocity, you need to predict the target’s position, which is done by estimating the position of the target based on its current heading, speed, and the time it will take for the bullet to get there.