- Home /
Question by
Experimen7 · Aug 14, 2013 at 04:19 PM ·
javascriptsmoothturning
Smooth turning
Here's the part of my code that drives movement on my player. It works fine, just that I couldn't figure out a way to smooth out the turning. It can get especially jerky because as the game goes on the player speeds up. So does anyone know how to smooth out the turning? Thanks in advance
function Update() {
transform.Translate(Vector3.forward*Time.deltaTime*playerSpeed);
if (Input.GetKey ("left")) {
transform.Rotate(0, -Time.deltaTime*turnspeed, 0);
}
if (Input.GetKey ("right")) {
transform.Rotate(0, Time.deltaTime*turnspeed, 0);
}
}
Comment
Answer by zombience · Aug 14, 2013 at 04:47 PM
Instead of transform.Rotate(), use something like:
Vector3 targetRotation = transform.rotation.eulerAngles;
if (Input.GetKey("left"))
{
targetRotation.y = -1;
}
if (Input.GetKey("right"))
{
targetRotation.y = 1;
}
transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(targetRotation), Time.fixedDeltaTime * turnspeed);
Answer by Geopig13 · Dec 04, 2014 at 08:06 PM
You could do it like this:
float targetRotation = Input.GetAxis("Horizontal");
transform.Rotate(0, targetRotation * 100 * Time.deltaTime, 0);
Your answer
Follow this Question
Related Questions
2d camera help! 0 Answers
How to make an animation finish after looping smoothly? 0 Answers
Smooth Rotation Help 1 Answer
character turning problem! 0 Answers
Move Object Smoothly 1 Answer