Look rotation makes sudden changes

This is a racing game, I use pathfinder for the character runs on a oval racetrack. Player can only play with the characters speed which is calculated and has a high limit. When the player reaches high range of speed character suddenly looks left or right for a moment and straightens back and its very annoying. Its not happening with low speed. What am i missing? The dir vector is calculated same with the tutorials. I also use simple smooth modifier.

            dir = (path.vectorPath [currentWaypoint] - transform.position).normalized;
    Vector3 rot = dir;
          rot.y = 0f;
 var targetRotation = Quaternion.LookRotation (rot, Vector3.up);
 transform.rotation = Quaternion.Slerp (transform.rotation, targetRotation, Time.deltaTime * speed * 0.3f);
 characterController.SimpleMove (dir * speed * 0.8f);

You should check

path.vectorPath [currentWaypoint] - transform.position

if it is a very small vector. If so, skip this simple movement.
For example, only move and rotate when rot.sqrMagnitude > 0.1.

2 Likes

I checked and increased the value up to 0,99 and still happens. Even remove adjustable speed from slerp like this:

Quaternion.Slerp (transform.rotation, targetRotation, Time.deltaTime * 10f);

still happening :pensive:

edit: it is all smooth and ok with speed 25 not 30 something or higher. I thought that it must about character controller, now im confused.

I found a solution:

When i checked the angles between dir and transform.position the values are like:

143.7691
143.8332
.
.
145.8065
.
.
52.40902 (eww!)
.
.
140.2589

I dont know why my direction vector changes suddenly. Now im checking for sudden angle change and it seems ok, probably not the best solution.