- Home /
Creating an animation with variables?
So I have been looking for a way to play an animation with different values. For example, I want to move my player from position 0 to position 100, and when I play the animation again, I want it to move from 25 to 75. I looked online and all I could find is suggestions to use a script for the movement instead of an animation. But I would like to know if there is a possible way to achieve this with an animation? Any help is very much appreciated.
Answer by Hellium · Nov 15, 2017 at 11:14 AM
The AnimationCurve
API provides function to set curves. I've not tested the script myself, but it's a starting point:
public AnimationClip clip ; // Drag & Drop the clip to change
void Start()
{
float startTime = 0 ;
float startValue = 0 ;
float endTime = 60 ;
float endValue = 100 ;
string objectName = "" ; // Leave empty if the target object is the one with the animator component
AnimationCurve curve = AnimationCurve.Linear( startTime , startValue , endTime , endValue );
string relativeObjectName = string.Empty; // Means the object holding the animator component
clip.SetCurve( relativeObjectName, typeof(Transform), "localPosition.x", curve );
}
Source :
Hello @Hellium and thank you very much for the answer. First of all, I would like to deeply apologize for the late reply as I was building the environment where I can test the code. Second of all, I tested the code and I tried to get it to work, but there are some things that I couldn't actually understand. for instance, how do we assign the game object using the string? so I have a parent object (the parent contains the animator) that contains the 2 children that should rotate randomly. So I changed the string objectName to the same name of my child object. After I start the game nothing happens with the animation. But when I check the animation window I find a new missing property with the name that I created. So the question now is how do I assign that gameObject?
Also, this is the warning that I get:
Could not register property modification for animation binding rotation.z of object Pointer, modifications will not be automatically reverted
Can you indicate the property you animate when calling clip.SetCurve
? It seems you can't animate world rotation (nor world position, nor world scale). You have to call :
clip.SetCurve( "Pointer", typeof( Transform ), "localRotation.x", curve );
Be careful when dealing with rotation,
The property that I am trying to animate is rotation.z
. And it's not (Transform), it is a (RectTransform) since I'm working on a canvas. After making the changes that you suggested, the error is gone. But weirdly, it immediately snaps to -180 in rotation, neglecting the curve's startValue and endValue.
Answer by Max_Bol · Nov 13, 2017 at 11:55 AM
It's possible to do it with an animation, but in the end you'l always have to work with a script that will manage the animation regardless. (Unless you create an insane system that includes hundreds of animations covering all the distances. Total waste of time.)
To "move" a player based on the animation, you got to use the Root Motion: https://docs.unity3d.com/Manual/RootMotion.html
This obviously requires you to have the animation with Root Motion (meaning that the animation includes the forward movement of each steps.) Then you can handle the animation while keeping track of the distance between the player and the target position through the script. The player than move according to the animation while orient itself and determines if it has to play the animation or not based on the script which check the distance and orientation of the player. This doesn't requires any "movement" of the player. Only Orientation & Distance calculation to be used as condition. (You can even handle the orientation through the animation, but you still need to script the detection of when the player is looking toward the right place.)
It's not really about the Root $$anonymous$$otion (Since my player is a just an object: ball, box, .... etc.), it's the part about randomizing the value that I'm trying to achieve. And of course, I will be using the script to deter$$anonymous$$e the random value. It's just how to you make to object move from and to these random values? I updated the Question with a picture to explain more.
Your answer
Follow this Question
Related Questions
root motion on in place animation 0 Answers
Horizontal axis not working after animation 1 Answer
How to combine anim states with movement in a random movement game. 1 Answer
How to prevent Homanoid Mocap Animation Z-axis movement? 0 Answers
How to make a model move during timeline in Cinemachine. 0 Answers