Search Unity

Scripted keyframe works for position but not rotation

Discussion in 'Scripting' started by GameLvr, Aug 21, 2010.

  1. GameLvr

    GameLvr

    Joined:
    Mar 22, 2009
    Posts:
    115
    I am trying to move object A (wherever it currently is) to object B's location and orientation by creating animation curves through scripting.

    The following code works exactly as expected for position, but not for rotation. Any ideas?

    Code (csharp):
    1. var targetA : GameObject;
    2.  
    3.  
    4. function Start ()  {
    5.  
    6. var startPx = transform.localPosition.x;
    7. var startPy = transform.localPosition.y;
    8. var startPz = transform.localPosition.z;
    9.  
    10. var startRx = transform.localRotation.x;
    11. var startRy = transform.localRotation.y;
    12. var startRz = transform.localRotation.z;
    13.  
    14. var endPx = targetA.transform.localPosition.x;
    15. var endPy = targetA.transform.localPosition.y;
    16. var endPz = targetA.transform.localPosition.z;
    17.  
    18. var endRx = targetA.transform.localRotation.x;
    19. var endRy = targetA.transform.localRotation.y;
    20. var endRz = targetA.transform.localRotation.z;
    21.  
    22. var curvePx = AnimationCurve.EaseInOut(0,startPx, 3, endPx);
    23. var curvePy = AnimationCurve.EaseInOut(0,startPy, 3, endPy);
    24. var curvePz = AnimationCurve.EaseInOut(0,startPz, 3, endPz);
    25.  
    26. var curveRx = AnimationCurve.EaseInOut(0,startRx, 3, endRx);
    27. var curveRy = AnimationCurve.EaseInOut(0,startRy, 3, endRy);
    28. var curveRz = AnimationCurve.EaseInOut(0,startRz, 3, endRz);
    29.  
    30.  
    31. // Create the clip with the curve
    32. var clip = new AnimationClip();
    33. clip.SetCurve("", Transform, "localPosition.x", curvePx);
    34. clip.SetCurve("", Transform, "localPosition.y", curvePy);
    35. clip.SetCurve("", Transform, "localPosition.z", curvePz);
    36.  
    37. clip.SetCurve("", Transform, "localRotation.x", curveRx);
    38. clip.SetCurve("", Transform, "localRotation.y", curveRy);
    39. clip.SetCurve("", Transform, "localRotation.z", curveRx);
    40.  
    41. // Add and play the clip
    42. animation.AddClip(clip, "test");
    43. animation.Play("test");
    44. }
    45.  
    46. @script RequireComponent(Animation)
    If I pause during runtime and tweek the rotation curve a teeny-tiny bit, the curve snaps to the expected easein/easeout. I know rotations get complicated math-wise with quaternions and eulers- do rotations need to be handled differently? I couldn't find any examples in the help.

    Thanks in advance!

    (fixed typos)
     
  2. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    You've got this part:-
    Code (csharp):
    1. var curveRx = AnimationCurve.Linear(0,startRy, 3, endRx);
    2. var curveRy = AnimationCurve.Linear(0,startRy, 3, endRy);
    3. var curveRz = AnimationCurve.Linear(0,startRy, 3, endRz);
    ...where you use startRy each time and this part:-
    Code (csharp):
    1. clip.SetCurve("", Transform, "localRotation.y", curveRx);
    2. clip.SetCurve("", Transform, "localRotation.y", curveRy);
    3. clip.SetCurve("", Transform, "localRotation.y", curveRx);
    ...where you use localRotation.y each time. (Copy/paste without remembering to change the variable names? ;-) )
     
  3. GameLvr

    GameLvr

    Joined:
    Mar 22, 2009
    Posts:
    115
    me cut paste??? <sheepish grin>

    I was just testing with the y value originally. So I fixed the bad bits and am still getting odd results- even when I set them all to EaseInOut. See the curves-
     

    Attached Files:

  4. GameLvr

    GameLvr

    Joined:
    Mar 22, 2009
    Posts:
    115
    OK, getting closer here...

    If I right click the curves in the Animation view, I can change the Interpolation to Euler Angles and the curves look ok.

    But the boxes still do not line up at the end. I've tried using rotation, localRotation, localEulerAngles and eulerAngles for the rotations, but I'm still missing something.

    It obviously needs the quaternians to get from point A to point B directly. And if I check on the final values they match the target, but the orientation doesn't.
     
  5. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    Can you post the code you've got currently?
     
  6. vzheng

    vzheng

    Joined:
    Dec 4, 2012
    Posts:
    45
    hi I got this question like you if I use localPosition.x in this
    it work but if i change it like this:
    it trun 90 var x help!!
     
  7. vzheng

    vzheng

    Joined:
    Dec 4, 2012
    Posts:
    45
    but the gameobject can trun 90 nomater how mush I set in the keyframe. help me plese。。。