Hello, I built a function C# function to calculate how much elevation my turret needs based on the force applied to the projectile, and it works wonders… if my tank is on a completely stable plane at 0,0,0 rotation (horizontal), but if i tilt it just 1 small bit, say from 0,0,0 to 0,1,0 it will start missing the target provided that the target is far enough…
This is because I need to set the rotation of the turret as well as the elevation, but I dont know how to do it, i have been searching but I cant seem to find the info i need much less the formula for it.
If any enlighten one could help me out would be greatly appretiated.
Here is the rotation code I have (velocity is the velocity i want it to rotate per update):
public static float CalculateRotationToTarget (GameObject myself, GameObject target, float velocity)
{
// Rotation (Yaw) of the turret
Vector3 targetVectorTurret = target.transform.position - myself.transform.position;
Vector3 localTurretHeading = myself.transform.InverseTransformDirection (targetVectorTurret);
float requiredYaw = Mathf.Rad2Deg * Mathf.Atan2 (localTurretHeading.x, localTurretHeading.z);
float deltaYaw = Mathf.Clamp ((requiredYaw / 10) * velocity, -45.0f, 45.0f) * Time.deltaTime;
return deltaYaw;
}
This might give you some ideas (its my elevation function) projectileSpeed normally is 80, elevationSpeed is how fast it should move up:
public static float CalculateElevationToTarget (GameObject myself, GameObject target, float projectileSpeed, float elevationSpeed)
{
float deltaPitch = -1000;
try {
// Pitch of the barrel
Vector3 targetVectorBarrel = target.transform.position - myself.transform.position;
Vector3 localBarrelHeading = myself.transform.InverseTransformDirection (targetVectorBarrel);
float requiredPitch = Vector3.Angle (Vector3.up, localBarrelHeading) - 90.0f;
float attackVelocity = projectileSpeed;
float distance = (myself.transform.position - target.transform.position).magnitude;
float y = target.transform.position.y;
Thanks in advance
Joao