How to handle smooth rotating?

Hey Guys.
I’ve got a problem with rotation. It looks like the whole objects hitting some kind of barrier.

It looks like this: cmb7d3

And here’s the code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    [SerializeField] private float _speed = 4f;
    [SerializeField] private float _smooth = 1f;

   
    [SerializeField] private GameObject _Player;

    public Quaternion newRotation;
    public Vector3 newPosition;

    // Start is called before the first frame update
    void Start()
    {
        newPosition = transform.position;
        newRotation = transform.rotation;
    }

    // Update is called once per frame
    void Update()
    {
        MovementController();
    }

    void MovementController()
    {

        if (Input.GetKey(KeyCode.W))
        {
            newPosition += (transform.forward * _smooth);
        }
        if (Input.GetKey(KeyCode.S))
        {
            newPosition += (transform.forward * -_smooth);
        }
        if (Input.GetKey(KeyCode.D))
        {
            newPosition += (transform.right * _smooth);
        }
        if (Input.GetKey(KeyCode.A))
        {
            newPosition += (transform.right * -_smooth);
        }

        if (Input.GetKey(KeyCode.Q))
        {
            newRotation *= Quaternion.Euler(Vector3.up * _speed);

        }
        else if (Input.GetKey(KeyCode.E))
        {
            newRotation *= Quaternion.Euler(Vector3.down * _speed);
        }

        transform.position = Vector3.Lerp(transform.position, newPosition, Time.deltaTime * _smooth);
        transform.rotation = Quaternion.Lerp(transform.rotation, newRotation, Time.deltaTime * _smooth);

    }


}

It looks to me that the issue is due to you adding to the newRotation too quickly. You are adding a set amount each, regardless of how long that frame took yet you are limiting the amount the object can rotate each frame dependant on the framerate.

To put it in numbers say the object starts at an angle of 0 degrees, you press the button and a few frames later target rotation is set to 90 degrees, the object starts turning right a limited amount, a few frames later when the object still hasn’t reached 90 degrees, the button has been held down and the target rotation is now 360 degrees, so the lerp then starts turning the object left towards it due to the lerp.

What I would do, is multiple your added rotation by Time.deltaTime so the amount being added is dependant on frame times and then do away with the lerp.

1 Like

@WarmedxMints_1 that was it! Thanks!