- Home /
Add animation curve through script
Hello,
I am trying to create for a GameObject a simple animation curve in script and add it to an animation clip defined in the prefab from which the GameObject is instantiated in run-time. The following code is used:
GameObject stoneToMove = GameObject.Find("StoneBlackX6Z12");
stoneToMove.animation.GetClip("StoneMovement").ClearCurves();
Keyframe[] frames = new Keyframe[2];
frames[0] = new Keyframe { time = 1 / 60, value = stoneToMove.transform.rotation.x };
frames[1] = new Keyframe { time = 11 / 60, value = stoneToMove.transform.rotation.x - 90};
AnimationCurve animationCurveRotationX = new AnimationCurve(frames);
stoneToMove.animation.GetClip("StoneMovement").SetCurve("", typeof(Transform), "localRotation.x", animationCurveRotationX);
Debug.Log(animationCurveRotationX.keys[1].value);
Code does not generate any errors. The Debug.Log correctly prints. Clip "StoneMovement" is defined on the prefab. Nevertheless, nothing happens. If i check the Animation Viewer for my object at run-time, the curves for Rotation properties are created but only one KeyFrame at time 0 per each exists. If i create the same curve in the viewer on my prefab and only call Play in code, everything works as expected.
Answer by rolandha · Mar 08, 2012 at 07:17 PM
I had the same issue. I tried to animate the asset, not the gameobject in the scene. they both had the same name. Is StoneBlackX6Z12 the name of the prefab in the project view? What is the name of the prefab in the scene (hierarchy view)? Also StoneBlackX6Z12? If so rename it in the scene to StoneBlackX6Z12_01 or something. Use this for animating. Like GameObject.Find("StoneBlackX6Z12_01");
Answer by Varaughe · Oct 12, 2013 at 08:51 AM
You might find this useful : http://u3d.as/content/rus-artur-pfa/runtime-curve-editor
Your answer
Follow this Question
Related Questions
Setting animation curves always makes 'curves' 1 Answer
Weapon customize system 1 Answer
Using multiple buttons in script 3 Answers
Strange issue changing the animation state 0 Answers
Parameterized Animation Clip 0 Answers