Artillery Turret aim problem

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

could it possible be a matter of telling it to aim at the target (Horizontally), and let the vertical aiming do the rest?

I’m not a coder, but I think that could possibly work. :confused:

That is exactly what I am doing, or maybe I didnt understand your solution.

Let me clarify, I am aiming horizontally toward the target with a simple lookat type method, the gun will rotate exactly towards the target, and then the elevation algoritm will kick in. This will cause the projectile to hit the target everytime if my tank is sitting on a completely flat surface :

Plane at rotation → (0,whatever,0) such as (0,0,0) or (0,180,0)
Doesnt matter its position, it will hit the target everytime (if the projectile has enough force ofc)

The problem is : when the rotation changes at X or Z it will stop hitting the target because the rotation will not compensate in relation to the elevation needed.

Explanation, if say, my projectile makes an arc from one living room chair to another (imagine the arc, it will be perfectly aligned with the target), now say I put a book under one of the legs of the chair where my tank is sitting on, it will tilt the chair slightly, and so will the arc it makes, right? Problem is, how do i compensate for that?

I have beeing trying solutions, like getting the current rotation of the tank and try adapting the rotation based on that, but I always fail at that, probably because my knowledge of quaternions and 3d rotations overall is very poor, so i am probably failing at relating one thing to another.

Thanks in advance for some helps or ideas :slight_smile:

Is the projectile a rigidbody object that you are firing by applying a force? Are you aiming it along the axis of the gun object after elevation? If so, can the post the part of the script where you actually elevate the gun (ie, where you change the rotation)? It is quite easy to set the local rotation relative to a parent object rather than in world space, which is what you need here.

Its a rigidbody i am applying force to yes.

Wont that solution cause the projectile to go somewhere else from where the turret is pointing to? If turret is poiting towards point X but the tank itself is rotated by 40 degree (max), if we calculate the projectile itself using 0 tilt, the projectile will actually turn somewhere the turret is not directly pointing to, right?

What I am saying is that if you set the gun’s orientation in world space, it should be the same regardless of how the tank is tilted.