Limit Rotation

Hi,

What would be the best way to limit this tracking rotation between 0 - 180 degrees ? I’ve used transform.eulerAngles to get the degrees of the transform and limited it between 0 - 180 but it stays at ether one end or the other.

Thanks.

function trackTarget(){

// Rotate towards target and track it	
	var targetPoint = target.position;
	var targetRotation = Quaternion.LookRotation (targetPoint - transform.position, Vector3.up);

	transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * trackSpeed);
	
	if(distanceToPlayer < attackRange){
	shoot();
	}
}

Angle should be based off target rotation, not the transform rotation.

var angle = targetRotation.eulerAngles.y;

Note the way this code is written, the rotation will stop when the object passes beyond the range even if the Slerp rotation has not fully rotated to the limit.

P.S. How did you embed formatted code into a comment?