- Home /
lerp to 0, problem with horizontal value
Hi,
I would like my character to go back to its original z rotation (straight) when I do not use the keyboard arrows anymore. But I think that, because there is still a value in the horizontal axis, the character continues to lean on the right or the left depending on which key i press. And then, when the value is at 0, it goes back to its rotation by default.
Would you know how to avoid this short time lapse and directly ask the character to go back to the original rotation in z?
Here is the code :
function Update () {
//rotate z keyboard
Debug.Log("horiz : "+Input.GetAxis("Horizontal"));
if ( Input.GetAxis("Horizontal") ){ /* leaning */
transform.Rotate( -Vector3.forward * 100 * Time.deltaTime * Input.GetAxis("Horizontal") );
transform.eulerAngles.z = ClampAngle(transform.eulerAngles.z, 10);
} else {
var m_lerp; /* returns to z rotation = 0 */
if (transform.eulerAngles.z < 180)
m_lerp = Mathf.Lerp(transform.eulerAngles.z, 0, 10 * Time.deltaTime);
else
m_lerp = Mathf.Lerp(transform.eulerAngles.z, 360, 10 * Time.deltaTime);
transform.eulerAngles.z = m_lerp;
}
Thanks
When editing angles, you should use $$anonymous$$athf.LerpAngle, it handles 360 degree problems...
Answer by aldonaletto · Feb 18, 2013 at 03:46 AM
Input.GetAxis returns a float, not a boolean - you should compare it to some limit, like this:
if (Mathf.Abs(Input.GetAxis("Horizontal")) > 0.01 ){ /* leaning */
...
This way anything out of the range -0.01..+0.01 will be considered "no key" condition. You can increase the limit if a faster response is desired - try 0.1, for instance.
Your answer
Follow this Question
Related Questions
Which functions to which update, got confused 0 Answers
Keep current rotation? 0 Answers
Cant slow down rotation Speed 1 Answer
Rotate object after swipe according to swipe direction 0 Answers