- Home /
Play an animation from the animator at a specific FRAME using anim.Play();
I'm trying to make it so my 2D rigged character aims his gun towards the mouse. I have an override layer with an animation called "Aim", that only affects the arm. It has 180 frames, each frame corresponding to a rotation value. (e.g. frame 90 = 90 degree rotation of the arm)
I need to set the "Aim" animation to a specific frame so that it will rotate. So far all I've found that could be useful is anim.Play(). Example of what I got so far:
anim.Play ("Aim", 1, 0.5f);
"Aim" being the animation name, of course. "1" is the layer, "0.5f" is the time in the animation for it to play. My problem here is that it goes from 0 - 1, not from 0 - max frames. So I don't know how to go to a specific frame using this function. I'm basically looking for a math equation that'd let me set the animation to a specific frame using the .Play() function. I know it's possible as I've done it before, but I don't have access to my old project files anymore. Can someone lend a hand? :/
Thanks, but isn't that using the animation component, not the animator? (which is what I'm using)
Answer by mightybob · Mar 07, 2017 at 02:25 AM
Answering my own question in case someone else is wondering. This is what I've figured out:
animator.Play ("AnimationName", 1, ( 1f / total_frames_in_animation ) * desired_frame);
Answer by NecrosDk · Mar 06, 2017 at 02:30 PM
I would look into making a generic animation for what is supposed to be displayed when your character aims at something, and then changing the transform of the gun part to look at your mouse, by scripting its rotation.
Something like this:
void Update () {
Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
transform.rotation = Quaternion.LookRotation(Vector3.forward, mousePos - transform.position);
}
It's important that the rotation is done by animations so I can have it look natural by blending animations when the character is looking around with his head or ai$$anonymous$$g with his arm.
$$anonymous$$g. head is looking at mouse but still is blended with the running animation so it still bobbles and doesn't look un-animated.
I don't know if it's sufficient for what you're trying to achieve, but let's say you want a character running, and while he runs, he bops the gun up and down. In that case, I would make a generic up and down bop animation on the y axis and rotate the gun physically, so that you only need one single animation. $$anonymous$$aybe I misunderstood your question..
Answer by unity_RJs1M1mPBz0Pxg · Dec 31, 2021 at 02:17 PM
ayo whats the second parameter equal to 1 for ? My thing returns me a warning.
Im mean what's the layer for I am so confused I didnt know layers applied to specific animation frames like that wth
Layers are used to blend animations between them, each layer has unique state, so one can be playing arms and hand animations and second can do legs for example, if you use -1 for layer, it means to look for the first animations that matches the provided name