How do I set the time of an animation playing in the animator (Mechanum)

I want my player to aim his gun at the cursor. To do this, I made an animation of him aiming from straight down to straight up on the animation scale of 0:00 to 1:00 (I am assuming that those are suppose to be seconds, but I am not really sure). I now want to set the time of the animation (how far along it is) so that he can go anywhere in the animation, depending on the cursor’s location.

I have the angle all figured out, and I know the time I want to set it to, but I can’t figure out what time variable I can change that I can get to when using mechanum, and not having a list of animation components attacked to an object. This means I cannot use animation["animation name"].time=time, in case that was not clear.

I also cannot use animator.GetCurrentAnimatorStateInfo(1).normalizedTime=time; , because normalizedTime is read-only.

I am pretty sure what I am looking for is AnimationState.time, but for the life of me, I can’t figure out how to get a reference to it when I only have a reference to the animator.

If I am doing this all wrong, what should I do to make my character be able to aim at the cursor? (other than set his transform by hand, of course)

Thanks in advance!

Finally, I would like to add that this is not a duplicate of either of the following because I cannot use animation["name"].time:

or

Chances are you’ve figured something out already, but this is for future viewers.

There’s no clear way to access AnimationState without having an Animation component on your object.
The only alternative I’ve found is to use, if you cannot use legacy/deprecated options:

Animator.Play("StateName", layer, normalizedTime);

The trick is finding a way to turn whatever value you are using into a number between 0 and 1. In my case of using a timer that is finding what amount though the time bar you want to be, then dividing it by the total time.

Hope this helps someone on their search.

For anyone looking for this as well; they’ve made this functionality a bit easier to access in the animator! There is a checkbox on your animation clip for ‘normalized time’ that uses any float variable you make in your animator as a time input. It’s a more direct way to do what Chris Holdem provided a way for also.

Referring to stuffkikker’s answer, note that Normalized Time in the animation properties now seems to be called Motion Time.

@Cafetorium is right. I had to perform the same task, aim a gun based on a number in an animated 3d model. What I did:

  1. In the animator, choose the state you need to control.
  2. In the inspector enable the property called “Motion Time”
  3. Link the ‘Motion Time’ toggle to an animator float parameter.
  4. From your script call the following: GetComponent().SetFloat(“Float parameter name”, animation expected time);

It works.

18 - is start time in seconds.

Animator animator;
void Start()
{
animator.Update(18);
}

or Random start time

Animator animator;
void Start()
{
    animator.Update(Random.Range(0, 18));
}

,18 - is start time in seconds.

animator.Update(18);

or

animator.Update(Random.Range(0, 18));

Try to use this Unity - Scripting API: Animator.speed for changing animation playback speed. It affects the current animation playing and all consequent animations so do not forget to return the value back to 1 when finished.