B
B
Bruh_Bruh2021-02-03 19:18:42
Unity
Bruh_Bruh, 2021-02-03 19:18:42

How to make turns smoother?

Turning at the touch of a button is instantaneous, which is logical, but how to make it smooth? So that the car turns not in one frame, but in one second? Inserting Time.deltaTime as the 2nd argument to the method in the Rotate method will be incorrect, because then the rotation will simply be small, not smooth.

public class CarMovement : MonoBehaviour
{ 
    public float speed;
    public float turnAngle;

    private void Update()
    {
        transform.Translate(Vector3.forward * speed * Time.deltaTime);

        if (Input.GetKeyDown(KeyCode.A))
        {
            CarTurnLeft();
        }
        else if (Input.GetKeyDown(KeyCode.D))
        {
            CarTurnRight();
        }
    }

    private void CarTurnRight()
    {
        transform.Rotate(Vector3.up, turnAngle);
    }

    private void CarTurnLeft()
    {
        transform.Rotate(Vector3.up, -turnAngle);
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Lesh48, 2021-02-04
@Lesh48

I advise you to use Vector3.lerp

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question