Rotating object along bezier curve: not rotating enough?
- by Paul
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) :
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