Smooth LookAt (object rotation, not camera)

Hello, I am using the built in transform.LookAt(/vector3 position/) and noticed that my object simply begins to look at that direction. What I need in a fast turn (or slow. doesn’t matter really).

Here is my code:

using UnityEngine;
using System.Collections;

public class movement : MonoBehaviour {

	[SerializeField]
	float _speed;

	int _waypoint_number = 0;

	[SerializeField]
	GameObject points;

	[SerializeField]
	float _topSpeed;

	[SerializeField]
	float _accelerationInput;

	[SerializeField]
	float _turningSpeed;
	
	Vector3 next_position;

	void accelerate(){
		if (_speed < _topSpeed) {
			_speed += _accelerationInput * Time.deltaTime;
		}
	}

	void Start () {
		Transform temp  = points.transform.GetChild(_waypoint_number);
		next_position = temp.transform.position;
	}
	
	void Update () {
		accelerate();
		transform.LookAt(new Vector3(next_position.x, transform.position.y, next_position.z));
		transform.Translate (0, 0, _speed * Time.deltaTime);
	}

	void OnTriggerEnter(Collider c){
		if (c.transform.name == ("p" + _waypoint_number)){
			_waypoint_number += 1;

			if (_speed > _turningSpeed + 10) {
				_speed = _turningSpeed;
			}
			Debug.Log(c);
		}
		Transform temp  = points.transform.GetChild(_waypoint_number);
		next_position = temp.transform.position;
	}

}

To explain my code a little, I’m using empty objects on my map with colliders set to (is trigger). When my car enters the collider, it changes its direction to face the next object. All of them are p0, p1, p2, p3, p4, etc so that I can just cycle through them infinitely. The car slows down as it enters a collider to make it more realistic.

Most of it is irrelevant, only the LookAt part really. I tried to slow down my car, but still get the ‘unrealistic’ turning effect. If anybody has any idea about this, I would appreciate it. My idea is this:

  • I get the position and store it in a variable.
  • I check if my car’s rotation matches the variable.
  • If not, then add +1 Rotation to it in the needed direction.
  • Over time, the car will level out with the direction.

But how do I do this? How do I achieve it using code? It would be easy if I didn’t have to deal with 2 positions (x and z) but only one. I know that I would have to use global Rotation, but I’m only able to use local (unless someone can teach me :D).

Here is an image of it:

I would recommend that instead of using transform.LookAt, you use the Quaternion.LookRotation function. This generates a rotation that you can assign instantly or lerp to. Example:

void SmoothLook(Vector3 newDirection){
    transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(newDirection), Time.deltaTime);
}

You can increase the last parameter of Quaternion.Lerp to speed up how quickly it moves, but I would recommend always multiplying by Time.deltaTime to make it framerate-independent.