- Home /
Limit rotation angle on Z axis while slerp
hii,..
I am doing slep of rotation and i want to set maximum rotation angle to -3 and minimum rotation angle to 17.
But i dont understand how do i set. code i have used for slep is here :
// Update is called once per frame
void Update ()
{
if (Input.GetMouseButtonDown (0)) {
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast (ray, out hit, 100)) {
if (hit.collider.name == "LeftCollider") {
finalRotation = Quaternion.Euler (0, 0, 5) * kite.transform.rotation;
if (kite.transform.eulerAngles.z < 14 || kite.transform.eulerAngles.z > 350) {
rotating = true;
StartCoroutine (rotateKiteDown ());
}
}
if (hit.collider.name == "RightCollider") {
finalRotation = Quaternion.Euler (0, 0, -5) * kite.transform.rotation;
if (kite.transform.eulerAngles.z < 16 || kite.transform.eulerAngles.z > 350) {
rotating = true;
StartCoroutine (rotateKiteUp ());
}
}
}
}
}
IEnumerator rotateKiteUp ()
{
while (kite.transform.rotation != finalRotation) {
kite.transform.rotation = Quaternion.Slerp (kite.transform.rotation, finalRotation, Time.deltaTime * 2.5f);
yield return 0;
}
kite.transform.rotation = finalRotation;
}
IEnumerator rotateKiteDown ()
{
while (kite.transform.rotation != finalRotation) {
kite.transform.rotation = Quaternion.Slerp (kite.transform.rotation, finalRotation, Time.deltaTime * 2.5f);
yield return 0;
}
kite.transform.rotation = finalRotation;
}
your code is far too much, try to just post the relevant few lines
Answer by Fattie · Nov 30, 2015 at 07:29 PM
Ekta. Conceivably, just use void LateUpdate()
and very simply look in there at the eulerAngles, and crush them down to your limits.
(So, quite simply like ...
e = ... eulerAngles
newZ = Mathf.Clamp( e.z, -3f, 17f)
e.z = new Z
.. eulerAngles = e
.. and you're done.)
It's quite difficult to fight against slerping, you could get flicker.
IT can be very, very difficult to achieve what you're up to, but using the "trick" of LateUpdate can work in certain ranges of motion. Hope it helps.
This is the same solution that I was about to give. :D In my experience, it did not work very well, but it mostly worked.
sir Can you post exact full code? and will it behave same like slerp on every hit ?
if (hit.collider.name == "RightCollider") {
finalRotation = Quaternion.Euler (0, 0, -5) * kite.transform.rotation;
Vector3 eulerAgles = finalRotation.eulerAngles;
float z = $$anonymous$$athf.Clamp (eulerAgles.z, -3.0f, 17.0f);
eulerAgles.z = z;
finalRotation.eulerAngles = eulerAgles;
kite.transform.rotation = finalRotation;
}
I have done something like this. But its not working smoothly like slerp. @Fattie
you can use slerp. Clamp rotation angle before calling coroutines. I didn't have any problem with slerp.