- Home /
 
Rotation along bezier curve: not rotating enough
Hi,
I tried to rotate my object as it moves along a bezier curve. But it does not rotate enough, the angle is too low, it goes up to 6 instead of 90 for example, as you can see on this image (the y eulerAngle is at 6, I would expect it to be around 90 with this curve) :

Would you know why it does this? And how to make the rotation toward the next point?
Here is the code : (I am comparing x and z to get the angle, and adding the angle to eulerAngles.y so that it rotates around the y axis)
 void Update () {
         //restart the movement
         if ( Input.GetKey("d") ) start = true;
         if ( start ){
             myTime = Time.time;
             start = false;
         }
         float theTime = (Time.time - myTime) *0.5f;
         if ( theTime < 1 ) {
             car.position = Spline.Interp( myArray, theTime );//creates the bezier curve
             counterBezier += Time.deltaTime;
             
             //compare 2 positions after 0.1f ** HERE **
             if ( counterBezier > 0.1f ){
                 counterBezier = 0;
                 cbDone = false;
                 newpos = car.position;
                 float angle = Mathf.Atan2(newpos.z - oldpos.z, newpos.x - oldpos.x);
                 angle += car.eulerAngles.y;                
                 car.eulerAngles = new Vector3(0,angle,0);
             }
             else if ( counterBezier > 0 && !cbDone ){
                 oldpos = car.position;
                 cbDone = true;
             }
 
               Thanks
Answer by robertbu · Oct 19, 2013 at 09:48 PM
The major issue is that Mathf.Atan2() returns radians, so you need to multiply the result by Mathf.Rad2Deg. But even with this fix, I don't think the rest of your code will give you want you want. Let me suggest an alternate solution:
 Vector3 v3 = newPos - oldPos;
 v3.y = 0.0f;
 transform.rotation = Quaternion.LookRotation(v3);
 
              Your answer