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? :confused:

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);

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);
     }

ayo whats the second parameter equal to 1 for ?
My thing returns me a warning.