How To Get Current Animation Name
Asked Answered
A

5

0

Hello Everyone

i’m trying to send Animation Throw RPC

but i have many Animation, so i will just try to make Shortcut way

.animation[CurrentAnimatinoName].speed = VlocityMagnitude / RunSpeed;

i want to get CurrentAnimatinoName.
can anyone help me please?

Acea answered 30/9, 2023 at 20:11 Comment(0)
V
0

There is no “current animation” because multiple animations can be playing at the same time. For example, you could be cross-fading between idle and run, or you could be playing a blend of walk animations (walk forward + strafe right) and also upper body animations such as waving, all at the same time.

I recommend keeping track of the animation yourself. For example:

string currentAnimation = null;
currentAnimation = "Run Forward";
animation.Play(currentAnimation);
animation[currentAnimation].speed = VelocityMagnitude / RunSpeed;

The code above is just an example based on your question that should get you started. But in practice you’ll probably want to implement something more robust.

Another approach would be to iterate through all the clips in animation[] and examine the weights. Hmm, I just noticed these have both suggested earlier in this question:

Vertebrate answered 30/9, 2023 at 20:11 Comment(1)

umm okey, so there is no way to Find the name of clip that already playing, u know because i have allot of animation and i need to send them by RPC so if i could found way to get the name will be allot easier, anyway thanks

Acea
B
0

Well everyone says you can’t because there can be multiple animations playing at the same time etc. But in my case i have just one animation per state and found a neat way to get currently playing animation without doing all IsPlaying() loop thing. It’s also precise. And i must state i’m doing this for legacy mode. Sorry if its too late and told before.

So you can call this once in start and add a specific event to each clip:

foreach (AnimationState state in animation)
{
    AnimationEvent animationEvent = new AnimationEvent();
    animationEvent.stringParameter = state.name;
    animationEvent.functionName = "SetCurrentPlayingAnimation";
    animationEvent.time = 0f;
    animation[state.name].clip.AddEvent(animationEvent);
}

Thus every single begining of your animation state you get the name of it. Then place method SetCurrentPlayingAnimation(string statename) in "yourclass" attached to your object (whatever it is) and you can set the result "state.name" to a property like currentState in it.

Borghese answered 30/9, 2023 at 20:10 Comment(0)
P
0

I guess that’s what you have been looking for.

private string CurrentPlayingAnimation
{
    get
    {
        if (this.Animation.isPlaying)
            foreach (AnimationState item in this.Animation)
                if (this.Animation.IsPlaying(item.name))
                    return item.name;
        return null;
    }
}
Process answered 30/9, 2023 at 20:9 Comment(0)
D
0

I prefer this:

Animator animator;
AnimatorClipInfo[] animatorinfo;
string current_animation;
  
void Awake()
{
    animator = GetComponent<Animator>();
}
  
void Update()
{
    animatorinfo = this.animator.GetCurrentAnimatorClipInfo(0);
    current_animation = animatorinfo[0].clip.name;
}
Disgruntle answered 30/9, 2023 at 20:10 Comment(0)
M
0
CurrentAnimatinoName = animator.GetCurrentAnimatorClipInfo(0)[0].clip.ToString();
Modesta answered 30/9, 2023 at 20:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.