Limiting Quaternion.Slerp based Rotation

Hello,

I am rotating an object using Slerp here’s my code.

//the touch

Vector3 centerToTouchDirection = new Vector3(touchPosition.x, 0.0f, touchPosition.y) - WORLD_CENTER_POINT;

Quaternion targetRotation = Quaternion.LookRotation(centerToTouchDirection.normalized);

//let's rotate

paddles[nearestPaddleIndex].gameObject.transform.rotation = Quaternion.Slerp(paddles[nearestPaddleIndex].gameObject.transform.rotation, targetRotation, Paddle.ROTATION_SPEED * Time.deltaTime);

Now I want to limit the rotation let’s say 0-180 degress. Any clues? Thanks! :slight_smile:

Okay, found out the answer. I have an object in the scene named “Paddle” and that paddle should rotate 0-180 degress. I just made a code in the LateUpdate() thingy, anyways here it is.

void LateUpdate() {

float clampedY = Mathf.Clamp(transform.eulerAngles.y, 15f,167f); //lets clamp the Y axis rotation(half circle) Assuming that the object is instantiated in the right-most middle part of the circle.

transform.eulerAngles = new Vector3(transform.eulerAngles.x, clampedY ,transform.eulerAngles.z); //let's rotate

}

Now you can just edit the clamp values.