I’ve fried my brain with this one and would very much appreciate any help anyone can give! Looked at similar questions and answers but can’t get my head around these rotations…
I have a tank with a random rotation on its turret (it sweeps left and right, changing direction randomly).
I have a missile spawner as a child of the turret with a script which aims and fires along the axis of the turret:
In F3DMissileLauncherSimplified.cs, attached to the MissileLauncher child, I’m using the following code in the Fire() method (this is randomly triggered so that the missiles launch at varying intervals):
Transform barrel = transform.parent;
float turretRotation = barrel.parent.parent.parent.localRotation.eulerAngles.z;
Quaternion dirAngle = Quaternion.Euler(0, turretRotation, 0);
Transform tMissile = F3DPool.instance.Spawn(missilePrefab, barrel.position, dirAngle, null);
The Spawn method seems pretty standard and is defined as follows:
public Transform Spawn(Transform obj, Vector3 pos, Quaternion rot, Transform parent)
The situation is slightly complicated by the fact that this is a Blender mesh imported with a 270 rotation on the x axis but all other transforms are aligned as you’d expect. (See screenshots above)
This all works fine when the tank is stationary but once I added a NavMeshAgent and a random roaming script, making the tank move around (in addition to the turret rotation) the missiles no longer come out in line with the turret, but off at varying angles.
I feel stupid. Surely getting the rotation angle, looking down a turret, can’t be this difficult!