- Home /
Predict where object will be
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.
 float aimAngle(Transform go, float bulletSpeed){
     float speed = go.FindChild("Logic").GetComponent<Character>().speed;
     float v = go.rotation.z * speed;
     float rCrossV = go.position.x * v - go.position.y * v;
     float magR = Mathf.Sqrt(go.position.x * go.position.x + go.position.y * go.position.y);
     float angleAdjust = Mathf.Asin(rCrossV / (bulletSpeed * magR));
     
     return angleAdjust + Mathf.Atan2(go.position.y, go.position.x);
 }
Just a question, Is it a no-go to actually implement a ho$$anonymous$$g feature ? - I'm asking, cause it's actually simpler to achieve than the prediction one (but i'm not saying it can't be done)
Answer by jokim · Aug 28, 2014 at 05:59 PM
Well, not directly answering your question, but here's how i'd approach this problem : (as I mentioned in a comment)
Have your bullet be a class. Which has a target that can be set. - most probably an enemy.
Now, instead of predicting where the target will be in X time, just go towards it. continuously. adjusting rotation every frame
When you create the bullet, give it a target, and in its update code, have something like :
 void Update()
     {
         transform.LookAt(target.transform);
         transform.Translate (transform.forward * speed);
     }
Just make sure your enemies never go faster than your bullets !
Answer by Doodlemeat · Aug 28, 2014 at 03:44 PM
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.
Would you $$anonymous$$d pointing to some illustrations ? I don't see the Right angled triangle you're describing... (I'm actually interested in this topic)
EDIT - After re-reading, I can only conclude that this does NOT define a right angled triangle in EVERY cases. Have a tower at (0,0), enemy at (10,10), Now if the bullet reaches the last enemy position (10,10) but the enemy only walked to (5,10) : How is this a right angled triangle ?
(0,0), (5,10), (10,10) does not correspond... I can't do Pythagoras on that.
I´ve edited my answer now. I took for granted that his tower defense game contained straight walkpaths. The solution wont work with diagonal paths.
actually... even that restriction doesn't make all right angled triangles... $$anonymous$$y triangle was all straight paths. and it wasn't right angled.
Your answer
 
 
             Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
2d game facing bullets to mouse 2 Answers
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                