Rotating object along bezier curve: not rotating enough?

Posted by Paul on Game Development See other posts from Game Development or by Paul
Published on 2013-10-19T21:27:34Z Indexed on 2013/10/19 22:17 UTC
Read the original article Hit count: 387

Filed under:
|
|
|
|

I tried to follow the instructions from the threads on the forum (Cocos2d rotating sprite while moving with CCBezierBy) with Unity, in order 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) :

enter image description here

Would you know why it does this? And how to make the rotation toward the next point?

Here is the code (in c# with Unity) : (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 () {
        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
            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

© Game Development or respective owner

Related posts about c#

Related posts about unity