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?