- Home /
 
 
               Question by 
               shrapnel92 · Jun 04, 2014 at 11:56 AM · 
                rotationclamplimit  
              
 
              Clamp rotation of a GameObject
Hi all, I've looked into it here and other places, but I can't seem to work it out. When I click my mouse, the object rotates on Y, but I'm trying to work out how to limit the rotation, I've been using Mathf.Clamp but I'm not having much luck. Can anyone point me in the right direction? I'm coding in C#.
     void Update () {
         if (Input.GetMouseButton (0)) {
                         Debug.Log ("Input recieved");
                         transform.Rotate (Vector3.up * Time.deltaTime * moveSpeed);
                 }
 
         if (Input.GetMouseButton (1)){
                         Debug.Log ("Input recieved");
                         transform.Rotate (Vector3.up * Time.deltaTime * -moveSpeed);
                 }
 
         transform.rotation = new Vector3(Mathf.Clamp(Transform.rotation.y, -90, 90));
 }
 }
 
              
               Comment
              
 
               
               
               Best Answer 
              
 
              Answer by pacific00 · Jun 04, 2014 at 12:15 PM
are u sure this is not throwing an error... ??? rotation is a quaternion and you are storing a vector in it..
try transform.localEularAngles = new Vector3( x , mathf.clamp( y ,..) , z );
That sorted it, thanks ! I'd mark that answer as correct but I don't have enough rep! :(
Your answer
 
             