Is there a way to access and set a GameObject’s transform component in an animation (in a script)? Can I change an object’s position at a specific time in the animation?
Yes you can change the value of an animated transform component but you need to do it in LateUpdate() which is called after the animation update. If you do it in Update() your value gonna get override by the animation system
Hi, is there a way to make a reference to an expecific frame of an animation? I want to make a function that change the positiojn of my Collider2D when the animation pass the 3° frame, then change it again passing the 7 frame, the 10 frame, the 15 frame, and there…
Thank you for your response. How can I get/set the value of a position of an object at a specific time in the animation frame?
there is many way to achieve this,
you can add two animation event on your animation clip to mark the beginning and the end of time when you want to set the position of your object.
you can poll at each LateUpdate the playback time of the currently playing clip and if you are in the time range that you want you set the value of your’s object
I’m looking at the AnimatorStateInfo, but I don’t see a variable for the position - so how would I be able to set this position at a certain time if I wanted to do so in a script?
I would recomend you to use AnimationEvents as they are designed for this kind of operation but here the code in case you want to do it with polling.
You need to poll the animator at each update and when normalizedTime is pass the specific time that you want you set the position on the transform that yout want.
class MyMonoBehaviour : MonoBehaviour
{
public GameObject myObject;
public Vector3 position;
void LateUpdate()
{
Animator animator = GetComponent<Animator>();
AnimatorStateInfo currentAnimatorStateInfo = animator.GetCurrentAnimatorStateInfo(0);
float playbackTime = currentAnimatorStateInfo.normalizedTime * currentAnimatorStateInfo.length;
if (currentAnimatorStateInfo.IsName("MyState") && playbackTime > 0.3f)
{
myObject.position = position;
}
}
}
Is LateUpdate() only used to change a position? What about if I know the positions at certain times in advance and just want to set them from the beginning - I’m guessing I wouldn’t need to poll the animator in that case - so what would I use to code that?
It depend if the position that you want to change is animated or not,
If it animated it does mean that the animator will write a value at each Update call so you need to override the animated value in a LateUpdate() call/
If it not animated then you can set the value once at the beginning and that it, you can do it in either Update() or Start() or LateUpdate() in this case it doesn’t matter
Great, and how would I set the position values from the beginning (in a case where I’m not overriding)?
something like this
class SetupPosition : MonoBehaviour
{
public GameObject myObject;
public Vector3 myValue;
void Start()
{
myObject.transform.position = myValue;
}
}
Sorry I wasn’t clear - I meant how would a set a position at a specific time (time I guess would be the animation time frame)?
not sure I understand what you want to do then, I did post the solution a few reply above to set the position at a specific time.
I’m trying to figure out a way to set specific positions at specific times - knowing that in advance (since I’ll have an external data file with that information). So I’m trying to do something like this:
- class MyMonoBehaviour : BallPosition
- {
- void Start()
- {
- if (playbackTime == 0.3f) //where playbackTime is a time in the animation frame
- {
- this.transform.position = new Vector3(0, 0, 0);
- }
- else if (playbackTime == 0.4f)
- {
- this.transform.position = new Vector3(1, 0, 0);
- }
- //…for multiple times in the animation frame
- }
- }
So where would I get this playbackTime to set where the object would go at a certain time?
@Mecanim-Dev do you know if it’s possible to do this?
AnimatorStateInfo currentAnimatorStateInfo = animator.GetCurrentAnimatorStateInfo(0);
float playbackTime = currentAnimatorStateInfo.normalizedTime * currentAnimatorStateInfo.length;
You cannot use the == operator, because there is no gurantee that you gonna get a tick exactly at 0.3f or 0.4f.
But you should use animationevent in this case if you have a lot of timeline event in your animation clip.
Hello,
this discussion is a little old but I was hoping to find an answer here to a question I seem unable to grasp.
I want a little Ball’s transform.position to be animated. Starting at one Vector2, passing another and ending at a third. However the three Vector2 positions are different every time I play the animation on the ball. How do I access the balls animation every time I want but set the keyframes to different values?
Thank you for support!