i need to fire a projectile
towards a position that is ahead of the enemy
and is further from or closer to
the enemy's position at the time of firing
based on the distance between player and enemy at that time
and the current velocity of the enemy (which is "1.0" when it is traveling forward normally, goes above "1.0" when turning on multiple axes)
included is an image
of the current behavior
it works pretty well
but I know I can target more accurately
math help or advice?
Pythagorean theorem? where to plug in which values? what do i solve for if targetahead, the vertice between a and b, is what i'm figuring out?
>>17949688
you need the speed and trajectory of the enemy object, then you need the speed of your projectile and the angle of the shot, if there are any factors like air resistance and gravity you have to factor this in too, if not its relatively easy:
If say the distance between you and the enemy is 1000 along the x axis and 200 meters along the y axis (so the distance is √(1000^2 + 200^2), and you are stationary and the enemy is moving towards you on the x axis at 10 meters/second while staying at the same height, and the speed of your projectile is 300meters/second then you can put it in a formula:
√(1000(-10s)^2+200^2) = 300s
solving s in this problem gives you the flight time of your projectile from launch to impact.
now that you know this flight time you multiply it with the speed of the enemy object to acquire a distance in meters, this gives you the lead on your target.
>>17949732
if you have gravity and air resistance then it becomes a little bit trickier, since these both affect the projectile. the air resistance and gravity will slow the projectile, to calculate the air resistance you need the square meters of the front of the projectile. to calculate the gravity you need the angle of your shot.
if you have your angle you can calculate how hard gravity will affects your shot, this is also done with the Pythagorean theorem. however since your projectiles trajectory will yearn towards the ground (gravity) your angle increases and so does the effect of gravity on the projectile.
>>17949732
also for a quick guess you can say the distance is like 1km, and speed is 300 m/s so 3 seconds flight time, so multiply the objects speed in seconds by 3 and this is your quick guess trajectory projection
>>17949732
would this be easier, knowing that my projectiles speed does not scale off of my ships speed, but just a fixed amount per frame?
>>17949732
also, how would I be getting "whether or not the enemy is moving towards me"??
the engine allows me to calculate "in front of wherever the current transform" is, which is that transform.forward here
void Start () {
player = GameObject.FindWithTag ("Player").GetComponent <Player> ();
Destroyshot ();
hasTarget = player.seesTarget;
if (hasTarget) {
target = player.locktarget;
targetahead += target.position + target.forward * (((Vector3.Distance(transform.position, target.position)) / 100) * (player.locktarget.gameObject.GetComponent<Rigidbody> ().velocity.magnitude));
transform.LookAt (targetahead);
Debug.Log ("" + player.locktarget.gameObject.GetComponent<Rigidbody> ().velocity.magnitude.ToString ());
}
}
>>17949767
if you can measure distance in frames then yes, you just have to put the amount of frames the enemy moves per second against the amount of frames your projectile moves per second, and then the amount of frames your projectile moves per second more than the ship is what you use to divide the distance with. this gives you a time unit which you multiply with the amount of frames the enemy moves and that is your lead.
>>17949782
>also, how would I be getting "whether or not the enemy is moving towards me"??
the direction is important because that affects the distance your projectile has to cover, if its moving towards u its shorter and if it moves away from u its longer
>>17949787
i agree, i am just trying to keep it as simple as possible, for modular-ability
with the camera work, the more directly an enemy is moving towards or away from the player, the less of a spread the chance to miss is when calculating using transform.forward, so i am okay with not accounting for that, for ease of scripting, and for how many of these calculations i plan to have going at once
your formula will be very helpful to me though, i appreciate it