I’ve been changing 2D animations from scripts by using the Animator class’s setInteger method and having a condition set for each animation transition based on this value. This allows me to set the transform position of my player’s gun (a child object) for each animation frame, which is necessary because the gun needs to correspond to the player’s hand position.
Is there any way to make this happen instantly within an Update() method? I am changing the animation if the player fires his gun, and the new animation needs to be in place in order to have the correct transform for the bullet starting position.
I know I could make this work by having an alternate configuration for the bullet starting position, but I would really like to make this work through my existing animation system for consistency.
I believe in your case is better to use triggers instead of ints (ie. Animator.setTrigger())… If you use a trigger and remove the transition time and it should work… Not sure if i’m fully understanding your problem thou…
I set a child transform’s Position.x and Position.y values for each keyframe in the animation keyframe editor in Unity. When I change the animation from a script, I need these Position values to update instantly during that Update invocation, which is not happening.
Setting transition durations to zero doesn’t seem to work; the bullet was spawning at the location that was set for the previously used animation. The bullet did not start spawning at the correct location until later game Update iterations, at which point the animation was clearly set.
I could look into using triggers, though they have to change the animation within the Update() script method and not wait for a later evaluation.
It seemed to end up jittering back and forth between animation states when I used setTrigger, not holding to the animation state set in the code. My script kept setting the Trigger, briefly showing the shooting animation, but it kept going back to the previous animation.
If I could figure out how to get the value I configured for the gun transform position in the first keyframe of the animation from a script, this might be an alternative; then the script would be using the animation configuration to set the bullet starting position.
Hey Again! I made you a small example showing a possible implementation of what you’re seeking. It combines triggered animations with no exit time and 0 transition time (Two triggers one for start shooting and one for ending the shooting
process) and the LateUpdate function to catch the first bullet that instantiates in the wrong position. let me know if this works for you!
Sorry for my slow response. It took me a couple tries to figure out what I was doing wrong.
The issue was that I needed to spawn my projectiles in the LateUdpate() method rather than the Update() method. The Update() method should set variables that allow the animation change to take place; by the time the LateUpdate() method is called, the animation will be changed, and the projectile spawning will be able to pull the correct firing position off the current animation. I did not have to use animation triggers to make it work.
webox, I may have found a bug in the Unity editor with its setting of additional attributes in animation frames; if you would be fine with me posting a modified copy of your project on the Unity bug tracker, I could report this to the Unity team. It’s possible they’ve fixed it in one of their patch releases, but I want to make sure, as the apparent bug is very tedious to deal with.
Thank you for your example – it was extremely helpful.