Passing parameters to animation
Hi all, new in the fórum, and new to Unity - it's an amazing tool!
but I can't get my head around this problem:
I'm trying to instantiate a prefab which triggers an animation that's supposed to inflate the object from nothing to a size passed through script variable. ie a cube that appears and resizes depending on the parameter passed from the application.
I've attached the animation to the prefab, and kicks off as expected (when object instantiated), but I don't see how I can override the transform.scale in the animation to a dynamic value passed by script.
Thanks for your wisdom! David
Answer by Verusoft · Jan 25, 2016 at 02:18 PM
Do you really need animation for that? Why don't you change scale of object in C# script. You should use something like this:
IEnumerator changePlayerScale(Vector3 newScale,float speed) {
while (Vector3.Distance(player.transform.localScale,newScale) > 0.1f) {
player.transform.localScale = Vector3.Lerp(player.transform.localScale,newScale,Time.deltaTime * speed);
yield return null;
}
player.transform.localScale = newScale;
}
to start animation call
StartCoroutine(changePlayerScale(new Vector3(4,4,4),10));
Thanks Verusoft. Your solution works for this particularly simple animation, though my question was more generic - I didnt express myself properl - I want to be able to take an .anim from my designer and be able to call it, passing few runtime variables to "adapt" the animation to the runtime situation
Regards, $$anonymous$$
Your answer
Follow this Question
Related Questions
Corgi Engine - Attack Animation Won't Play From Idle 1 Answer
Why can't I delete animator parameters? 2 Answers
Issue with animation parameters, they're not there! 1 Answer
Animations created in Runtime 0 Answers
Animation with parameters 1 Answer