- Home /
AnimationClip.SetCurve "localEulerAngles" not working
Hello everyone.
I have another issue with the SetCurve function.
I'm trying to set a rotation curve along the Z axis.
Here is my line of code:
clip.SetCurve("objectname", typeof(Transform), "localEulerAngles.z", rotZcurve);
The data I'm putting in the curve is defiantly not empty.
When I open the Animation editor (this has nothing to do with mecanim),
I get the "Clean Up Leftover Curves" Button.
When I click it, I see all the Z rotation I'm trying to do....
Any ideas why unity think my Z rotation is empty?
Thanks!
Edit
Appearntly it has somthing to do with EulerAngles.
As soon as a change to localRotation, the curve does appear.
The problem is, localRotation uses quaternions.
No matter what I tried to do, I always get the rotation jumping between -180,0 and 180....
Can anyone help me figure it out?
Answer by dorpeleg · Jul 11, 2013 at 08:08 AM
Figured it out.
Apparently, when setting a quaternion rotation, you need to provide the values for the vector4.
So here is what I did:
var rotXcurve = new AnimationCurve();
var rotYcurve = new AnimationCurve();
var rotZcurve = new AnimationCurve();
var rotWcurve = new AnimationCurve();
var angle = Quaternion.Euler(0, 0, NeededRotation);
rotXcurve.AddKey(Frame, angle.x);
rotYcurve.AddKey(Frame, angle.y);
rotZcurve.AddKey(Frame, angle.z);
rotWcurve.AddKey(Frame, angle.w);
clip.SetCurve(GO.name, typeof(Transform), "localRotation.x", rotXcurve);
clip.SetCurve(GO.name, typeof(Transform), "localRotation.y", rotYcurve);
clip.SetCurve(GO.name, typeof(Transform), "localRotation.z", rotZcurve);
clip.SetCurve(GO.name, typeof(Transform), "localRotation.w", rotWcurve);
Hope this will help anyone.
Sir, you have definitely helped me a lot. I have been trying to figure this out for quite some time. Thank you.
Thank you! This helped me for some cases. However, there are some rotations values that doesn't work propertly, for instance Quaternion.Euler(100.23f, 46.05f, 0.53f) results in (79.76999, 226.05, 180.53) as keyframe, the direction is wrong I think... There is anyway to solve this?
Answer by David-Flook · Jun 19, 2019 at 01:02 PM
Instead, you can use the following property names to set the curve in euler angles like you were attempting to do originally.
localEulerAnglesRaw.x
localEulerAnglesRaw.y
localEulerAnglesRaw.z
thank you so much you are the best :)
I did it and it worked :)
void Update(){
if(Input.GetKeyDown(KeyCode.W)){
changeXPos();
}
if(Input.GetKeyDown(KeyCode.S)){
changeRot();
}
}
public void changeXPos()
{
Keyframe[] keys;
keys = new Keyframe[3];
keys[0] = new Keyframe(0,0);
keys[1] = new Keyframe(1,5);
keys[2] = new Keyframe(2,18);
var curve = new AnimationCurve(keys);
clip.SetCurve("wa", typeof(Transform), "localPosition.x", curve);
}
public void changeRot()
{
Keyframe[] keys;
keys = new Keyframe[3];
keys[0] = new Keyframe(0,0);
keys[1] = new Keyframe(1,5);
keys[2] = new Keyframe(2,15);
var curve = new AnimationCurve(keys);
clip.SetCurve("wa", typeof(Transform), "localEulerAnglesRaw.z", curve);
}