TowerDefense Game, shooting at Target!

Hey there, i know this question has been asked a few times here, but none of these answers helped me.

I have the following information:

`
float speed = target.GetComponentInChildren().getCurrSpeed();

Vector3 direction = target.GetComponentInChildren().getCurrDirection();

Vector3 enemyPos = new Vector3(target.position.x,target.position.y,target.position.z);

Vector3 towerPos = new Vector3(transform.position.x,transform.position.y,transform.position.z);

Vector3 LookAt = enemy - tower;
`

Is there a simple way of doing the math?
Thank you!

The easiest calculation I can think of is an approximation of the aim position. I didn’t do any tests but it should be something like:

  • find the time the bullet needs to reach the target (tBullet = sBullet / vBullet)
  • find the distance the enemy can travel in that time (sEnemy = vEnemy * tBullet)

Now because the tower rotates to that position sBullet will change and so tBullet does. You would need to repeat that calculation for more accuracy, but I guess it will be good enough if you execute the calculation only once.

Don’t know if there is an easier way.

You can combine your solution with a guided missile which automatically follows the target. So you can be 100% sure, that your missile hits the target, even how komplex, tortuous or chaotic your path is.

Usually tower defense games cheat instead of really calculating. If your bullet is fairly fast, they usually just put a script on the bullet to follow it. Something like this:
transform.lookAt(enemyPos);

And move the object forward or add velocity to the rigidbody of the bullet if any.
And add maybe a destroy condition, if the bullet traveled too far from tower in case you have enemies witch are too fast to shoot at.