- Home /
How to create animations that can be edited with scripts in runtime?
Okay, let's make it simple. I have 3 objects (A,B,C) on a plane. I made a script that permits me to swap their position with the mouse, so I can drag an object over another one, and swap their position.
This is very easy to accomplish with a script. But how to do it if I want an animation?
When I create an animation, I can define a starting position, and I final position, following a curve. I want to be able to change the final position in runtime, so I don't have to be worried where is the other player to swap, I can be sure to have that animation with that curve, from A to B or C.
Can you help me to accomplish this very simple task?
I can use timelines, if needed.
Working on Unity 2018.2.1f1.
Thank you.
Answer by seant_unity · Aug 03, 2018 at 11:12 AM
One suggestion is to use AnimationCurves to store the animation data in your script. You can copy and edit them at runtime to modify the final position.
From what I saw in the scripting manual, the AnimationCurves help you to define the type of movement of each animation, as well as adding new keys.
There is no mention of any "properties" (like transform.position) in the animation.
no, there isn't. You would need three curves, one for x, y and z. And then do something like
t += Time.deltaTime;
transform.position.x = curveX.Evaluate(t);
transform.position.y = curveY.Evaluate(t);
transform.position.z = curveZ.Evaluate(t);
each frame in your script.
AnimationClips are baked at runtime, so they can't be changed. But animation curves, which are a building block of AnimationClips, can be serialized and stored separately.
SOLVED.
I created a clip in runtime, created my three curves, and then I applied those three curves to my clip. Everything worked great!
Thanks for helping me out! ;)
Your answer
Follow this Question
Related Questions
Help with animator controllers 1 Answer
why I have to anim.getComponent in update() function when I had done in Start () function 2 Answers
Animator and Scripting problems. Help !! 1 Answer
Character Animator controller/movement 1 Answer
Is there a new retargetting system for the animation in 5.5? 1 Answer