Search Unity

Look rotation makes sudden changes with character controller

Discussion in 'Scripting' started by vantilator, May 29, 2015.

  1. vantilator

    vantilator

    Joined:
    Jan 6, 2015
    Posts:
    2
    This is a racing game, I use http://arongranberg.com/astar/ 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 astar tutorials. I also use simple smooth modifier for the path. Rotation is all smooth and ok with speed 25 not 30 something or higher. I thought that it must be about character controller.


    Code (CSharp):
    1.  
    2. dir = (path.vectorPath [currentWaypoint] - transform.position).normalized;
    3. Vector3rot = dir;
    4. if (rot.sqrMagnitude > 0.99f) {
    5.       rot.y = 0f;
    6.       vartargetRotation = Quaternion.LookRotation (rot);
    7.       transform.rotation = Quaternion.Slerp (transform.rotation, targetRotation, Time.deltaTime * 10f);
    8. }
    9. characterController.SimpleMove (dir * speed * 0.8f);
    10.  
     
    Last edited: May 29, 2015
  2. Duugu

    Duugu

    Joined:
    May 23, 2015
    Posts:
    241
    My guess: the charactercontroller moves beyond the current waypoint on high "Speed". Which causes it to "look back"on rotating to the waypoint.
     
    vantilator likes this.
  3. vantilator

    vantilator

    Joined:
    Jan 6, 2015
    Posts:
    2
    I found a solution:

    When i checked the angles between dir and transform.position the values were 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.
     
  4. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    890
    Are you sure, that the distance to the new point is far away enough? As I do not know your scene setup, it is just a guess, that, if your distance getting closer, your player turns to the point and gets that weird number?