- Home /
The question is answered, right answer was accepted
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. ][1]
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!
I need to make sure the bullet hits the Enemy. So when I use LookAt he aims directly on the enemy and the bullet fails, so i have to aim a little bit in front of the target, relative to the direction of the target and the speed of the target.
Is the movement of the bullet linear with a constant speed or does it decelerate?
no, it is linear, it just translates along the z axis with currentSpeed and Time.deltaTime
Thank you Robhuhn! It is working perfectly!
my code is like this:
float bulletSpeed = 20;
float enemySpeed = target.GetComponentInChildren<EnemyScript>().getCurrSpeed();
Vector3 enemyDirection = target.GetComponentInChildren<EnemyScript>().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);
float distance = Vector3.Distance(enemyPos,towerPos);
float tBullet = distance / bulletSpeed;
float sEnemy = enemySpeed * tBullet;
enemyPos += enemyDirection * sEnemy;
top.LookAt(enemyPos);
Thank you very much you helped me alot!
Answer by robhuhn · Nov 14, 2013 at 01:47 PM
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.
Thank you, I will give it a try and post the result here!
Glad that it works but be aware that the distance you add to the enemy direction should give the position on the path. That way it will also hit if the enemy moves around a corner. Now you assume the enemy walks straight
enemyPos += enemyDirection * sEnemy;
Say the path has a total of 20 meter and the enemy is at 10 meter (0.5 ratio) in front of a corner. The aim position should be 1 meter ahead, then get the position on the path at a ratio of 0.55 (11 meter)
Answer by ma_tech · Nov 14, 2013 at 02:07 PM
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.
Answer by gajdot · Nov 14, 2013 at 02:06 PM
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.
I thought of that before, but I also have a $$anonymous$$issleTower, the missles really use LookAt(targetPos) to never fail, but this is really not what I want to on my BulletTowerScript :(
I see, but now I'm a bit confused. If you want to make sure that your bullet hits, then you just need to speed it up a bit and hit a bit in front of your enemy, then use the same lookAt or lerp method after it traveled a bit, this way you hit it anyway. Otherwise if you need to miss your target time to time just use something like robhuhn suggested. That will miss your target in corners anyway.