- Home /
Calculate and apply trajectory formula to AI enemy weapon
My enemy ships have cannons that rotate up and down around their x axis, with the barrels pointing down the z axis.
I am trying to use the typical Slerp statement that you would use to smoothly rotate an object from the current rotation to the target rotation. I usually use Slerp to rotate around the Y axis, but am trying to alter it to use the x axis. I'm trying to calculate the target rotation by using a formula to find the required angle to reach the distance of a target:
Here is my code:
var distance = Vector3.Distance(transform.position, target.position);
var reqAngle = .5*Mathf.Asin((gravityV*distance)/(bulletSpeed*bulletSpeed));
turretOne.transform.rotation = Quaternion.Slerp (turretOne.transform.rotation, Quaternion.LookRotation(targetDirection), TurretRotationSpeed * Time.deltaTime); turretOne.transform.eulerAngles = Vector3(0, turretOne.transform.eulerAngles.y, 0);
cannonOne.transform.rotation = Quaternion.Slerp (cannonOne.transform.rotation, Quaternion.LookRotation(Vector3(reqAngle,targetDirection.y,0)), TurretRotationSpeed * Time.deltaTime); cannonOne.transform.eulerAngles = Vector3(cannonOne.transform.eulerAngles.x, targetDirection.y, 0);
The Cannons are attached to the turret. The turrets can target the player character just fine, I left that code out. But I can get the cannons to adjust to the proper angle to shoot their projectiles the correct distance.
Thanks!
Answer by mlkielb 1 · Sep 07, 2010 at 02:27 PM
I finally got it. Incase anyone want to do the same thing here is the code I used. I didn't get the Slerp working, but am not to concerned about it right now.
var bulletSpeed = 55.0; var gravityV = -9.5; var reqAngle = .5*Mathf.Asin((gravityV*distance)/(bulletSpeed*bulletSpeed)); reqAngle = .75*(reqAngle*100);
turretOne.transform.rotation = Quaternion.Slerp (turretOne.transform.rotation, Quaternion.LookRotation(targetDirection), TurretRotationSpeed * Time.deltaTime); turretOne.transform.eulerAngles = Vector3(0, turretOne.transform.eulerAngles.y, 0);
cannonOne.transform.eulerAngles = Vector3(reqAngle, turretOne.transform.eulerAngles.y, turretOne.transform.eulerAngles.z);
I had to cut the orriginal result of the angle buy 3/4 because of the enemy model setup. That's why I multiplied by .75. The cannons are attached to the turrets, that's why I included part of the turret code into the code.
What do u mean the enemy model setup? Is that because you scaled down the enemy model or not?
I don't think the scale of the object would matter. I was referring to other factors such as the height of the turret compared to the target that weren't factored into the equation. This equation is for two points that are perfectly equal height. If the orrinal trajectory equation works for you, you don't need to mess with the result like I did. $$anonymous$$y enemies were slightly over shooting their targets, so I multiplied the result by .75 to lower the cannons a bit.
Answer by jlcnz · Mar 18, 2012 at 06:22 AM
The reason this doesnt work, is because the result is in radians rather than degrees.
reqAngle * 180 / Pi
Your answer
Follow this Question
Related Questions
AI Turret Aiming 2 Answers
Shoot mortar on clicked floor 1 Answer
Projectile trajectory 1 Answer