- Home /
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:
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.
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.
Answer by Tom_Bombardir · Sep 25, 2021 at 09:50 AM
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
I've tried to clamp serveral different values but I don't think I understand enough about euler/quaternions to make any real progress, thank you though.
transform.rotation = Quaternion.LookRotation (new Vector3 (Mathf.Clamp(rotateVector.x, $$anonymous$$XDegree, maxXDegree), Mathf.Clamp(rotateVector.y, $$anonymous$$YDegree, maxYDegree), rotateVector.z), transform.parent.up);
Again, not sure if this will work and if this is what you need, but you can try
The value RotateTowards returns seem to be a Vector3 with each axis ranging between negative one, and positive one (though rarely going higher than naught point one). I've tried multiple combinations of positive and negative, large and small values for the $$anonymous$$/max degrees but no luck so far.
Vectors and quaternions remain a mystery to me, thank you once again though.