Clamping turret rotation on Y axis not working
Hello everyone !
Here is my issue, I have a gun, I am trying (I technically succeeded) to clamp its rotation on the Y axis between -35 and 35 degrees, and I mean it works, but for some reason this -35 / 35 degrees angle is behind the gun ??
I want it to be in front of it, not behind, like 0 degrees should be in front of the object, I don't understand haha
Here is my code :
ray = cam.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2, 0)); // Shooting where the crosshair is at, middle of the screen
pointingAt = ray.origin + ray.direction * 100;
if (turret)
{
//Adjusting the turret
tmpRotation = turret.localRotation;
targetPointTurret = (pointingAt - turret.position).normalized;
targetRotationTurret = Quaternion.LookRotation(targetPointTurret, turret.parent.transform.up);
if (gunTraverse == 0)
targetRotationTurret = Quaternion.Euler(targetRotationTurret.eulerAngles.x, targetRotationTurret.eulerAngles.y, targetRotationTurret.eulerAngles.z); // Updating the final rotation
else
{
// Clamping the left right axis of the gun
clampedY = targetRotationTurret.eulerAngles.y; // Getting the axis I want to clamp
if (clampedY > 180) clampedY -= 360; // We need that
clampedY = Mathf.Clamp(clampedY, -(gunTraverse / 2), gunTraverse / 2);
targetRotationTurret = Quaternion.Euler(targetRotationTurret.eulerAngles.x, clampedY, targetRotationTurret.eulerAngles.z); // Updating the final rotation
}
turret.rotation = Quaternion.RotateTowards(turret.rotation, targetRotationTurret, Time.deltaTime * GameGO.instance.activePlayerGO.unit.TurretRotatingSpeed);
turret.localRotation = Quaternion.Euler(tmpRotation.eulerAngles.x, turret.localRotation.eulerAngles.y, tmpRotation.eulerAngles.z);
}
here's a pic of how it looks :
Your answer
Follow this Question
Related Questions
How to detect if a GameObject is currently rotating? 1 Answer
Changing character X axis rotation so that gun points at target issue 0 Answers
Turret rotation based on its own local rotation. 3 Answers
Assign an object rotation along a single axis from other object's rotation. 0 Answers
Object with fixed position but follows camera rotation 0 Answers