Hi I’m new to unity, and C# in general. But I’ve made some decent progress but i’m a long way off from totally understanding everything i’m stitching together from various tutorials (Quaternion calculations and radons etc just completely allude me).
Basically i have tank, the turret need to rotate towards the target on the Y axis, and the gun barrel then needs to elevate to the target + the extra needed for the trajectory. I’ve actually done this and works fine. [pic0]
My issues:
- Some tanks need to have limited turret rotation &| barrel elevation ( eg tank destroyers vs turreted tanks).
- I have two workings versions of the turret rotation script, the first is commented out. Now the first is actually better, because it locks the turret to the parent axis and prevents tilting or elevating, which is what i actually want - but I just cant seem to incorporate this into a workable autoaim [pic1]. Whereas version two (not commented out works, but is cheating by keeping the turret horizontal to the world at all times [pic0]
- oh and finally, as a bonus i’d love someone to tell me how i can incorporate drag into my ballistic calculator so i’ll have a variable i can divide the penetration value to for shots a long range. Ideally i’d like to set “2” to the drag field in the projectiles rigidbody and then incorporate “2” into the ballistic calculator. But i fear this going to require some advance mathematics based on distance, and while i understand (barely) the wiki page on the subject, im not sure how i can combine both calculations into a single formula.
So here’s my code:
void Update () {
if (target != null) {
/*
Quaternion qTurret;
Transform trans = turret;
float distanceToPlane = Vector3.Dot(trans.up, target.transform.position - trans.position);
Vector3 planePoint = target.transform.position - trans.up * distanceToPlane;
qTurret = Quaternion.LookRotation(planePoint - trans.position, trans.up);
trans.rotation = Quaternion.RotateTowards(turret.transform.rotation, qTurret, (moveSpeed * 10) * Time.deltaTime);
Vector3 OurPos = this.gameObject.transform.position;
float angle1 = scripts.GetComponent<BallisticCalc> ().CalculateTrajAngles (shellVelocity, OurPos, target.transform);
barrel.transform.localEulerAngles = new Vector3 (angle1, 0, 0);
*/
Vector3 OurPos = this.gameObject.transform.position;
float angle1 = scripts.GetComponent<BallisticCalc> ().CalculateTrajAngles (shellVelocity, OurPos, target.transform);
Vector3 destDir = target.transform.position - turret.position;
destDir.y = 0;
Quaternion targetRotation = Quaternion.LookRotation (destDir);
turret.rotation = Quaternion.RotateTowards (turret.rotation, targetRotation, (moveSpeed * 10) * Time.deltaTime);
barrel.transform.localEulerAngles = new Vector3 (angle1, 0, 0);
}
}
and my ballistic calculator:
public float CalculateTrajAngles(float velocity, Vector3 start, Transform target){
float v = velocity;
Vector3 targetVector = target.position - start;
float height = targetVector.y;
targetVector.y = 0;
float x = targetVector.magnitude;
float y = height;
float g = Mathf.Abs(Physics.gravity.y);
float top = v*v + Mathf.Sqrt(v*v*v*v - (g*(g*x*x + 2*y*v*v)));
float bottom = g*x;
float angle1 = Mathf.Atan2(bottom,top);
return -angle1 * Mathf.Rad2Deg;
}
Any help appreciated.