Scripted keyframe works for position but not rotation

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?

var targetA : GameObject;


function Start ()  {

var startPx = transform.localPosition.x;
var startPy = transform.localPosition.y;
var startPz = transform.localPosition.z;

var startRx = transform.localRotation.x;
var startRy = transform.localRotation.y;
var startRz = transform.localRotation.z;

var endPx = targetA.transform.localPosition.x;
var endPy = targetA.transform.localPosition.y;
var endPz = targetA.transform.localPosition.z;

var endRx = targetA.transform.localRotation.x;
var endRy = targetA.transform.localRotation.y;
var endRz = targetA.transform.localRotation.z;

var curvePx = AnimationCurve.EaseInOut(0,startPx, 3, endPx);
var curvePy = AnimationCurve.EaseInOut(0,startPy, 3, endPy);
var curvePz = AnimationCurve.EaseInOut(0,startPz, 3, endPz);

var curveRx = AnimationCurve.EaseInOut(0,startRx, 3, endRx);
var curveRy = AnimationCurve.EaseInOut(0,startRy, 3, endRy);
var curveRz = AnimationCurve.EaseInOut(0,startRz, 3, endRz);


// Create the clip with the curve
var clip = new AnimationClip();
clip.SetCurve("", Transform, "localPosition.x", curvePx);
clip.SetCurve("", Transform, "localPosition.y", curvePy);
clip.SetCurve("", Transform, "localPosition.z", curvePz);

clip.SetCurve("", Transform, "localRotation.x", curveRx);
clip.SetCurve("", Transform, "localRotation.y", curveRy);
clip.SetCurve("", Transform, "localRotation.z", curveRx);

// Add and play the clip
animation.AddClip(clip, "test");
animation.Play("test");
}

@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)

You’ve got this part:-

var curveRx = AnimationCurve.Linear(0,startRy, 3, endRx); 
var curveRy = AnimationCurve.Linear(0,startRy, 3, endRy); 
var curveRz = AnimationCurve.Linear(0,startRy, 3, endRz);

…where you use startRy each time and this part:-

clip.SetCurve("", Transform, "localRotation.y", curveRx); 
clip.SetCurve("", Transform, "localRotation.y", curveRy); 
clip.SetCurve("", Transform, "localRotation.y", curveRx);

…where you use localRotation.y each time. (Copy/paste without remembering to change the variable names? :wink: )

me cut paste???

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-

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.

Can you post the code you’ve got currently?

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!!

but the gameobject can trun 90 nomater how mush I set in the keyframe. help me plese。。。