The following image illustrates my problem:[![enter image description here][1]][1]
The following image illustrates the scenario:
[![Illustrative Scenario][2]][2]
Final Edit 1: I implemented John Hamilton's solution, but then ran into the issue that it would only work for certain angles. I then saw ens' answer, and gave it a try, because it seemed to resolvesolution provided by @JohnHamilton latest update solves this issueproblem with perfect precision. I have implemented the following code, but it might be flawed, because θ stays at 85º regardless of where the objective is located at. This isnow removed the code:
public class AimCorrection : MonoBehaviour
{
public Transform target;
public Transform turretForward;
public Transform weaponForward;
private float d;
private float a;
private float alphaAngle;
private float betaAngle;
private float deltaAngle;
private float thetaAngle;
void Update()
{
a = Mathf.Abs(turretForward.position.x - weaponForward.position.x);
Debug.Log("a = " + a);
d = Vector2.Distance(new Vector2(target.position.x, target.position.z), new Vector2(transform.position.x, transform.position.z));
Debug.Log("d = " + d);
deltaAngle = 5f; //hardcoded at the time being
Debug.Log("Delta = " + deltaAngle);
alphaAngle = Mathf.Asin(a * Mathf.Sin(deltaAngle) / d);
alphaAngle *= Mathf.Rad2Deg;
Debug.Log("Alpha = " + alphaAngle);
betaAngle = 180f - alphaAngle - deltaAngle;
Debug.Log("Beta = " + betaAngle);
thetaAngle = Mathf.Asin(a * Mathf.Sin(deltaAngle) / d) + deltaAngle - 90f;
Debug.Log("Theta = " + thetaAngle);
}
}
And below is an image showing my setup as well as the results for each variable.
[![Setup and Results][3]][3]
For reference:
The red line is a capsule I created that represents the forward direction of the turret. The blue line represents a parallel line to the red capsule, only located at the weapon's base position. The pink line is a capsule I createdimages that represents the forward direction of the turret's weapon. The green object represents the target.
The turret is located at (0, 0, 0). The target is located at (0, 0, 10).
"a" represents the distance, in the X-axis, between the turret's base and the turret's weapon. It's 0.8 units away.
"d" represents the distance between the turret's base and the target. In this simple scenario, it's value is 10.
"δ" is the angle variation between the turret's weapon and the turret itself. In the represented scenario, it's 5º. Changing this value did not affect my results at all.
While this is a 3D environment, height does neither interests nor affects my situation, and thus I make calculations based on a 2D space.
Regardless of where I place the target, θ stays within decimal range of 85º.
Edit 2: This is my implementation of John Hamilton's solution, with the screenshots showing a before / after of applying the equation's resulting angle, in both a scenario in which it is correct, and one in which it doesn't hold.
The code:
public class TurretAimCorrection : MonoBehaviour
{
public Transform target;
public Transform turretForward;
public Transform weaponForward;
public float weaponRotation;
private float y;
private float x;
private float parallelAngle;
private float actualAngle;
void Update()
{
y = Mathf.Abs(turretForward.position.x - weaponForward.position.x);
x = Vector2.Distance(new Vector2(target.position.x, target.position.z), new Vector2(transform.position.x, transform.position.z));
parallelAngle = Mathf.Asin(y / x) * Mathf.Rad2Deg;
actualAngle = parallelAngle + weaponRotation;
Debug.Log("Angle: " + actualAngle);
}
}
Example of this code working: [![Working Correctly][4]][4]
Example of this not working correctly: [![Incorrect Angle][5]][5]
In both cases, the only thing that changed was the turret's Y-axis rotation, and the target's position. In both cases, the target is alignedused to the turret's forward vector.
In the first case, the target's position is (0, 0, 10). In the second case, the target's position is (10, 0, 0).
The weapon's rotation regarding the turret is set at 5º (coincidentally, the same number resulting from the equation).
All other turret / target in-between values are alsoillustrate my incorrect, it is just more noticeable in these extremes implementations. [1]: https://i.sstatic.net/Sr4qv.jpg [2]: https://i.sstatic.net/sLPoV.png [3]: https://i.sstatic.net/atgVe.png [4]: https://i.sstatic.net/OpQrC.png [5]: https://i.sstatic.net/gcYbJ.png
