How to make object turn smoothly without big amount of waypoints ?

I need movement without shaking at the turn. I tried to make a lot more waypoints, but there was no less shaking. Making speed of RotateTowards() helps, but I would like to do it using less amount of waypoints.


I used this script:

public class WayPoints : MonoBehaviour {
     // put the points from unity interface
     public Transform[] wayPointList;
     public int currentWayPoint = 0;
     Transform targetWayPoint;
     public float speed = 4f;
     // Use this for initialization
     void Start () {
     }
 
     // Update is called once per frame
     void Update () {
         // check if we have somewere to walk
         if(currentWayPoint < this.wayPointList.Length)
         {
             if(targetWayPoint == null)
                 targetWayPoint = wayPointList[currentWayPoint];
             walk();
         }
     }
     void walk(){
         // rotate towards the target
         transform.forward = Vector3.RotateTowards(transform.forward, targetWayPoint.position - transform.position, speed*Time.deltaTime, 0.0f);
         // move towards the target
         transform.position = Vector3.MoveTowards(transform.position, targetWayPoint.position,   speed*Time.deltaTime);
         if(transform.position == targetWayPoint.position)
         {
             currentWayPoint ++ ;
             targetWayPoint = wayPointList[currentWayPoint];
         }
     }
}

3123538--236506--output.gif

What you want is a bezier curve, heres a thread with a bunch of info.

1 Like

Or if you’re willing to hand over some money on the asset store there are a number of good spline tools.

1 Like

You can also try using Lerp/Slerp

I think your current code is actually almost good for making a smooth turn - that’s the point of the RotateTowards code - except that you need to feed in a much smaller number for the third parameter. Don’t use the same “speed” variable for moving and rotating. Make a separate “rotateSpeed” variable, and set it to something small. Note that that function takes radians (full circle = 6.28 radians), so a reasonable turn speed would be in the neighborhood of 1 rad per second (your current code does 4 rad per second, so its turn speed would be very fast).

This also won’t change the direction he moves, which will still “snap”, and he won’t be moving the same direction as he’s facing. To fix that, you could use this:

transform.Translate(0f, 0f, speed * Time.deltaTime);

which will walk him forward in whatever way he’s facing (which will be turning towards the target).

Be advised that you would then need to add logic besides == for checking for the next waypoint, since it’ll never exactly equal the waypoint’s position from one frame to the next. You have a couple of options, and since I don’t feel like typing them out, you can read more about them here (check the “nearest approach” section in particular).

1 Like

Thanks, I managed to make it look good using curve made in Blender and setting speed to 0.7f.