Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Rotation/following with new animation API

Discussion in 'Scripting' started by twintower31, Aug 22, 2008.

  1. twintower31

    twintower31

    Joined:
    Oct 31, 2007
    Posts:
    89
    I have an array that defines the positions across the time of the character.
    I want my character rotates correctly according to the motionpath defined by the
    curvex.AddKey(time,x)
    curvey.AddKey(time,y)
    curvez.AddKey(time,z)

    How can I implement the rotation ?
    - I wrote in the Update()

    Code (csharp):
    1.  
    2. void Update() {
    3. float t=animation[myanimationname].time;
    4. float nextx=curvex.Evaluate(t+0.1f);
    5. float nexty=curvey.Evaluate(t+0.1f);
    6. float nextz=mycurvez.Evaluate(t+0.1f);
    7. Vector3 target=new Vector3(nextx,nexty,nextz);
    8. transform.LookAt(target);
    9. }
    10.  
    But the character spins off and the rotation seems not to be very precise.

    I also tried to build first an array that contains the all direction vectors of the motionpath, and then
    Code (csharp):
    1.  
    2. Quaternion qrot=Quaternion.LookRotation(vectorDirectionNext - vectorDirectionCurrent,Vector3.up);
    3. curverotx.AddKey(time,qrot.x);
    4. curveroty.AddKey(time,qrot.y);
    5. curverotz.AddKey(time,qrot.z);
    6. curverotw.AddKey(time,qrot.w);
    7.  
    But it doesn't work.
    Does a better way exists ?
    I suppose the processing of the rotation curve is better in terms of performance.

    Thanks,