Rotation snapping to 180 degrees with transform.rotation
I'm directly getting input from my Horizontal axis and assigning it to quaternion.x which is then set as the rotation to my object. but even the slightest change in the quaternion (less than 0.00001) snaps the rotation to 180 degrees on any axis. Here's my code:
steer = Input.GetAxis("Horizontal");
localRotation.x = steer;
transform.rotation = localRotation;
Im trying to get the axis to directly controll the degrees of rotation on x axis.
I have two solutions in $$anonymous$$d. I will do the extra work for you, just let me know one thing: Do you want this to rotate for as long as you hold the button down, or do you want this to have a max angle of rotation?
Are you aware that the X-, Y-, Z-values of a quaternion do not correspond directly to euler-angles?
With maximum angles. I tried that with $$anonymous$$athf.clamp, but it changed nothing. im trying to copy a mobile device tilt axis to the angle of the game object with limits. THe tilt axis stuff is all worked out, its just the rotation itself that doesnt behave as expected.
Answer by JadeTheFlame · Jun 25, 2016 at 03:42 PM
Alright I have two answers for you.
First, is the one you asked for, using a maximum angle for your object. The drawback with using this one, is that you will not have control over how quickly your Input.GetAxis accellerates to 1 or -1.
float maxAngle = 120;
float steer;
void FixedUpdate ()
{
Quaternion steer;
steer = Quaternion.Euler(Input.GetAxis("Horizontal") * maxAngle, 0,0);
transform.localRotation = steer;
}
This second method simply rotates in the direction you are holding down, based on a fixed speed. Now, you can obviously alter this to your liking with an if statement to measure against the current rotation, so it has the potential to be a better solution if you need that rotation speed.
float rotationSpeed = 10;
float steer;
void FixedUpdate()
{
steer += Input.GetAxis("Horizontal") * rotationSpeed;
transform.localRotation = Quaternion.Euler(steer, 0, 0);
}
Let me know how that goes for you. Good luck!
The first one works but its a bit jerky, i think i'll use somekind of treshold or lerp it to position. Anyways, Thanks for the help!
Your answer
Follow this Question
Related Questions
My rotation is not the same in inspector as it is in the scirpt 0 Answers
Cant stop object/ridgidbody from rotating 2 Answers
Rotation is broken 2 Answers
3D, Rotate gun towards mouse 0 Answers
Cannon rotation clamping 0 Answers