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:
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:
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.
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.
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.