- Home /
Limit rotation Smoothly
Hello, I'm making a limitation for the rotation of a car, but when I clamp the rotation and gets to the limit rotation, the object goes to start switching between the limit rotation and 0. Thats my script.
public void LimitarRotacion() {
     if (transform.eulerAngles.x > 25)
     {
         rotationPosX = Mathf.Clamp(rotationPosX, 0, 24);
         transform.eulerAngles = new Vector3(rotationPosX,transform.eulerAngles.y,transform.eulerAngles.z);
     }
     if (transform.eulerAngles.x < -25)
     {
         rotationNegX = Mathf.Clamp(rotationNegX, 0, -24);
         transform.eulerAngles = new Vector3(rotationNegX, transform.eulerAngles.y, transform.eulerAngles.z);
     }
    
     if (transform.eulerAngles.z > 45)
     {
         rotationPosZ = Mathf.Clamp(rotationPosZ, 0, 45);
         transform.eulerAngles = new Vector3(transform.eulerAngles.x, transform.eulerAngles.y, rotationPosZ);
     }
     if (transform.eulerAngles.z < -45)
     {
         rotationNegZ = Mathf.Clamp(rotationNegZ, 0, -45);
         transform.eulerAngles = new Vector3(transform.eulerAngles.x, transform.eulerAngles.y, rotationNegZ);
     }
     
 }
Answer by unity_J016ZU_VZQzYAg · Mar 07, 2019 at 03:11 PM
I think you need to switch the values for Mathf.Clamp.
You are using 0 for min value, -45 for max value, but -45 is less than 0. So -45 should be the min value, and 0 the max value. rotationNegX = Mathf.Clamp(rotationNegX, -24, 0); rotationNegZ = Mathf.Clamp(rotationNegZ, -45, 0); 
Also what is the value of rotationNegX and all the other variables before you call the ifs ? Try using the current value of transform.eulerAngles.x and clamp that to save in rotationNegX rotationNegX = Mathf.Clamp(transform.eulerAngles.x, -24, 0);
Of course you would have to do that with the other ifs too. 
Answer by ByBarxX · Mar 07, 2019 at 06:11 PM
I've change it, but it still happen, when it gets to the limit rotation, the goes to 0 automatically.
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                