# Limiting RotateTowards on two axes

Hello,

I'm trying set the rotation of a child transform to that of a vector pointing in the direction of a target transform, with limits on the min/max rotation on each axis (X and Y).

Currently I'm using the code below to both limit the rotation of the target direction vector and set the rotation of the child transform:

`Vector3 targetVector = Camera.main.transform.position - transform.parent.position; Vector3 rotateVector = Vector3.RotateTowards (transform.parent.forward, targetVector, 50f * Mathf.Deg2Rad, 0f); transform.rotation = Quaternion.LookRotation (rotateVector, transform.parent.up);`

While this works and limits the max rotation of the child transform to fifty degrees on both the X and Y axis ideally I would like to be able to set a minimum and maximimum rotation value for each axis separately.

Using the parent transforms forward vector as zero degrees and referencing the parent transform up vector and the direction to the target vector:
1. I’d like to be able to limit the child transforms rotation on the X axis to a minimum of negative forty five degrees to a maximum of seventy degrees.

2. I’d like to be able to limit the child transforms rotation on the Y axis to a minimum of negative forty degrees to a maximum of ten degrees.

I've created two GIFs to hopefully better explain my intentions, the first GIF from a side view and the second GIF giving a top down view:

In both GIFs the coloured lines and arc segments represent:

• The pink cross is the target transform.
• The orange line is the direction to the target transform vector.
• The solid blue line is the forward vector of the child transform.
• The solid green line is the up vector of the child transform.
• The dashed blue line is the forward vector of the parent transform.
• The dashed green line is the up vector of the parent transform.
• The brighter green arc represents the ideal min/max rotation of the target vector.
• The darker green arc represents the current limited rotation of the target vector.
• The brighter red arc represents the ideal min/max rotation of the target vector.
• The darker red arc represents the current limited rotation of the target vector.

I'd like to preemptively apologize for the word mess above (easily the worse text editor I've ever used) and say any help would be greatly appreciated, thank you.

Not sure if that’s what you need, but you can try Mathf.Clamp. It limits the value going beyond a certain point with minValue and maxValue