- Home /
Smoothing the characters rotation with Lerp
Hey! I have a character, which will turn to face a certain direction when not moving. Currently the rotation after the character stops moving happens very fast, which looks ugly. So I'm trying to smooth it out with Lerp.
I've tried to code it but its currently not working, it doesn't give any errors but some of you might see whats wrong here.
public Quaternion dir; public float smoothSpeed = 0.5f;
void Update () { if(Input.GetAxis("Vertical")==0 && Input.GetAxis("Horizontal")==0) { dir.y=-135;
transform.rotation = Quaternion.Lerp(transform.rotation, dir, Time.deltaTime * smoothSpeed);
}
}
}
The direction is right, but it is still doing the rotation instantly.
Answer by AngryOldMan · Feb 27, 2011 at 10:53 PM
change
transform.rotation = Quaternion.lerp
to
transform.rotation = Quaternion.Slerp
Doesn't seem to help. I tried with different time values too but nothing seems to help. Anyone else notice anything wrong here?
Ok got it working, seems that I had a problem with the time value on Slerp, works with both, Lerp and Slerp. I didn't realize that the time value was between 0 and 1. Thanks for the help anway. :)
Answer by servival · Jun 23, 2015 at 06:20 AM
Here is the Final Code:
using UnityEngine;
using System.Collections;
public class Smoothrotatoin : MonoBehaviour {
public Quaternion dir;
public float smoothSpeed = 0.5f;
void Update () { if(Input.GetAxis("Vertical")==0 && Input.GetAxis("Horizontal")==0) { dir.y=-135;
transform.rotation = Quaternion.Lerp(transform.rotation, dir, Time.deltaTime * smoothSpeed/100);
}
}
}