- Home /
Getting the rotation of an object, rotates the object!
Hello everyone! I have a globe that the user will rotate: Here's the code: function Update () {
var horiz:float = Input.GetAxis("Horizontal");
var vert:float = Input.GetAxis("Vertical");
if(horiz > 0)
transform.Rotate(Vector3(0,1,0), Space.Self);
if(horiz < 0)
transform.Rotate(Vector3(0,-1,0), Space.Self);
if(vert > 0)
transform.Rotate(Vector3(0,0,1), Space.World);
if(vert < 0)
//print(transform.rotation.eulerAngles.z);
transform.Rotate(Vector3(0,0,-1), Space.World);
}
So for the vertical rotation, I will put a limit(ex: 10 degrees max) and no limit (full rotation horizontally). I have used the print to check/get the rotation, but instead it rotates the globe without a keyboard input, as I run the game! someone tell me why?
Any chance you could post an exact copy of the file you are testing? I wrote up the code above and things were good for me. I can post my exact code as a solution should you need it. I removed your comment line to clean it up a bit.
$$anonymous$$y guess is you have the deadzone / sensitivity set wrong on the input device set as "Horizontal" and "Vertical". The GetAxis() return values may be returning a non-zero value which in your code would cause rotation.
That problem solved! but I can't put a limit for the rotation.. if(vert > 0) { print(transform.rotation.eulerAngles.z); if(transform.rotation.eulerAngles.z < 15); transform.Rotate(Vector3(0,0,1), Space.World); }
The print works! I see it printing and the value becomes > 15 but the rotation doesn't stop
Answer by torrente · May 23, 2012 at 06:22 PM
If you want to stop the rotation from going beyond a minimum/maximum, you can clamp it. For example:
float rotationY;
float minimumY;
float maximumY;
rotationY += Input.GetAxis("Mouse Y");
rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
transform.localEulerAngles = new Vector3(-rotationY,0,0);
It's a little different than your design, but it will work. Assign the value, then clamp it. Here's the link to the unity page: mathf.clamp
Your answer