So I'm trying to calculate an object's future position so my enemy can land to it safely. Idea is that using the future position I can calculate the angle and convert it to a direction vector and also using distance I can calculate the speed necessary to reach its target. I'm still kind of stuck with this idea.

Here is code which I'm using at the moment.

// Find the close platform to land
Transform _tmp = GetClosestPlatform(SpawnerNew.Platforms);
// Hard coded time, should be time which enemy should take to reach position
float _constNeededTime = 3f;
// As my game's speed always increasing every frame so I need to calculate it also how it's in future (GameManager.Instance.OverAllSpeedModifier is called 10x times / sec)
float _anticipatedLevelSpeed = GameManager.Instance.CurrentLevelSpeed + (GameManager.Instance.OverAllSpeedModifier * _constNeededTime * 10);
// trying to calculate the future postion of the platform
Vector3 _anticipatedLoc = _tmp.transform.position - new Vector3((_platformSpeed * _anticipatedLevelSpeed) + _constNeededTime, 0f, 0f);
// Calculate angle
float _angle = Mathf.Atan2(transform.position.y - _anticipatedLoc.y, transform.position.x - _anticipatedLoc.x) * 180 / Mathf.PI;
// Convert angle to vector direction
_nearestPlatformDir = new Vector3(Mathf.Cos(_angle * Mathf.PI / 180f), Mathf.Sin(_angle * Mathf.PI / 180f), 0);
// Trying to calculate speed needed to reach platform
_neededSpeed = Vector3.Distance(_anticipatedLoc, transform.position) / _constNeededTime;


1 Answer 1


Forget trigonometry. Use vector math. Think of it as solving an equation: you want the position of the platform and the position of the enemy to be the same at some point in the future. So you write the equations of motion for both, and solve for the velocity of the enemy:

platform_x = platform_x0 + vel_platform0 * t + accel_platform * t^2 / 2
enemy_x = enemy_x0 + vel_enemyx * t
enemy_y = enemy_y0 + vel_enemyy * t

Note that I've written those assuming that the platforms accelerate over time, as you describe in your post. The enemy, by contrast, will move at constant velocity towards the platform.

Now in order to hit the platform, we need the values to be the same for some time T:

platform_x(T) = enemy_x(T) => 
platform_x0 + vel_platform0 * T + accel_platform * T^2 / 2 = enemy_x0 + vel_enemyx * T

platform_y(T) = 0 = enemy_y(T) =>
0 = enemy_y0 + vel_enemyy * T

Notice that we have three variables to solve for (T, vel_enemyx, vel_enemyy) and only two equations. This makes sense, as we can have the enemy move really quickly towards the platform, and hit it earlier, or move more slowly and hit it a bit later.

In your example, you fixed the time T to 3 seconds, so substituting that you can then easily solve vel_enemyx and vel_enemyy.

EDIT: Using your variables, code would be something like:

float vel_enemyx = 1/_constNeededTime * (_tmp.transform.position.x + GameManager.Instance.CurrentLevelSpeed * _constNeededTime + GameManager.Instance.OverAllSpeedModifier * 10 * _constNeededTime * _constNeededTime / 2 - transform.position.x);
float vel_enemyy = -transform.position.y / _constNeededTime;

You must log in to answer this question.