- Home /
Accurate Projectile Leading for AI [Third person shooter]
Here are two versions of projectile leading I found, one works somewhat but only if the player is at the correct distance and velocity. It's very unadaptable in that sense.
Vector3 targetRot = Player.transform.position + new Vector3(playerVelocity.x, playerVelocity.y * 0f,playerVelocity.z) * (predictRotation) - transform.position;
Quaternion toRotation = Quaternion.LookRotation(targetRot);
transform.rotation = Quaternion.Lerp(transform.rotation, toRotation,RotSpeed * Time.fixedDeltaTime);
The other one comes from unity wiki (This is not working me at all and not sure why but the AI just rotates sideways and stays there. The player's velocity and transform posiition is being updated as well so I'm not sure what's happening).
https://wiki.unity3d.com/index.php/Calculating_Lead_For_Projectiles
Any help with this is appreciated.
Here are the variables which I think belongs in the script. To make things less complicated the player will have a static move speed of 5f so no complicated velocity increase over time. It is actually not using a rigid body but a character controller.
-Distance float from enemy to player -Projectile Speed