I have a clamped turret that tracks and works fine. However, when it is tracking one direction and has hit its max rotation and the target “flips” to other side, the turret doesn’t want to rotate the other way to get to the target. Instead it is trying to rotate the shortest path but can’t because it is clamped. I wasn’t sure how to explain it, so I fired up MS Paint before MS kills it and made a quick and dirty picture explaining it:
Below is part of the script that manages the yaw:
relativePosition = yawSegment.InverseTransformPoint(targetTransform.position); angle = Mathf.Atan2(relativePosition.x, relativePosition.z) * Mathf.Rad2Deg; targetRotation = yawSegment.rotation * Quaternion.Euler(0f, Mathf.Clamp(angle, -yawSpeed * Time.deltaTime, yawSpeed * Time.deltaTime), 0f); yawSegment.rotation = Quaternion.RotateTowards(yawSegment.parent.rotation * yawSegmentStartRotation, targetRotation, yawLimit);
Does anyone have a simple solution to this? I am open to changing how the turret rotates. Thanks in advance!