Problems with finding the target direction to rotate to (Taken from FPS Microgame)

Based on the Unity FPS Microgame I duplicated and took the model from it to add a turret to my project. I’m currently having issues with getting turret to look at the target with my revised code and don’t know why.

Pic1:The turret pivot stops at the side rather than its forward.
Pic2:Proof that turret pivots forward Z axis is correctly rotated defaulting at 0.

My Code:

    private void LookAtTarget()
    {
        Vector3 directionToTarget = (_blackBoard.TargetAimPos - _turretPivot.position).normalized;
        Quaternion newRot = Quaternion.Slerp(_currentRotation, Quaternion.LookRotation(directionToTarget), (_state == State.Default ? _lookAtRotationSharpness : _shootingRotationSharpness) * Time.deltaTime);
        _turretPivot.rotation = newRot;
        _currentRotation = _turretPivot.rotation;
    }

Original Code:

                bool mustShoot = Time.time > m_TimeStartedDetection + detectionFireDelay;
                // Calculate the desired rotation of our turret (aim at target)
                Vector3 directionToTarget = (m_EnemyController.knownDetectedTarget.transform.position - turretAimPoint.position).normalized;
                Quaternion offsettedTargetRotation = Quaternion.LookRotation(directionToTarget) * m_RotationWeaponForwardToPivot;
                m_PivotAimingRotation = Quaternion.Slerp(m_PreviousPivotAimingRotation, offsettedTargetRotation, (mustShoot ? aimRotationSharpness : lookAtRotationSharpness) * Time.deltaTime);
               
                // shoot
                if (mustShoot)
                {
                    Vector3 correctedDirectionToTarget = (m_PivotAimingRotation * Quaternion.Inverse(m_RotationWeaponForwardToPivot)) * Vector3.forward;

                    m_EnemyController.TryAtack(turretAimPoint.position + correctedDirectionToTarget);
                }

I think an important piece of the code in the original is in the start function but I chose not to add it because I dont know what it actually does.

        m_RotationWeaponForwardToPivot = Quaternion.Inverse(m_EnemyController.GetCurrentWeapon().weaponMuzzle.rotation) * turretPivot.rotation;

Is this whats needed to determine a transforms forward?

9767724--1399446--1375542-33b067ae8ae871050eba147fbfe7ee90.png

I believe I found the reason, the turrets forward in the model is actually not forward but rather on its side. I will try creating an empty game object as a parent to correct the pivot and see.

I ended up adjusting the models pivot manually in a blender so its fixed.

2 Likes