Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Can you check if a clips is currently playing?

Discussion in 'Animation' started by petey, Apr 18, 2018.

  1. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,770
    Hi there,

    I have a case where I have one animation clip that I need to interrupt with the same clip.
    So I made two identical clips. If one is currently playing I'd like to crossfade to the other.

    Is there a way to see if a clip is currently playing? I couldn't find anything...

    Thanks,
    Pete
     
  2. theANMATOR2b

    theANMATOR2b

    Joined:
    Jul 12, 2014
    Posts:
    7,790
  3. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,770
    Thanks man, I’ve been searchin’ just those answers are so old I kinda hoped there’d be some better way to access info on clips than just iterating through every clip and comparing names and weights.
    I might have to wrap some of these things into methods and hide them away :)
     
  4. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,770
    Okay, after more searching I don't think it's actually possible.
    The first example gives you info on the current state, it doesn't appear to be possible to get info on other states or even a list of states that are in the animator.
    Second example is for legacy (It had me confused for a while). Maybe they are calling the new system 'Playables' so they don't add another variation on animation into the mix.
    I'm at a loss with this one.
     
  5. alexchesser

    alexchesser

    Joined:
    Sep 15, 2017
    Posts:
    147
    I'm trying to look into this myself too. I haven't found anything yet. Some theories I have about things that might work include taking the animation CLIP and setting an event on it. Maybe one that calls a function.

    I've worked with animation events before and they're OK https://docs.unity3d.com/Manual/animeditor-AnimationEvents.html but if you're dealing with an imported FBX you actually have to make a duplicate of the animation clip before you can add events to it. Which can be problematic in the scope of larger projects. Since if a clip changes and you need to reimport an FBX you have to manually re-adjust the imported clip. (Assuming the clip changed in the 3D editor)

    Though while searching for the above link I also found this https://docs.unity3d.com/Manual/AnimationEventsOnImportedClips.html which may make it easier to add events to imported clips. I can't test until next week to find out for sure.

    Anyways - if you fire off an event to a function which sets a Boolean (CLIP_X_IS_PLAYING) or a string (CurrentClip = "CLIP_X") that might help you achieve your goal. Though it seems like a lot of overhead and maintenance on that front. I'd love to find a more elegant way.
     
  6. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,770
    Hey thanks Alex for posting that!, I definitely hadn’t thought of events as an option.
    I still don’t understand how such a fundamental thing is not possible. Surely there must be a lot of people tripping up on this??
    I had to move onto some other tasks (because I was wasting too long on this) but I’ll post here if I manage to find a better solution.
     
  7. teutonicus

    teutonicus

    Joined:
    Jul 4, 2012
    Posts:
    67
    theANMATOR2b likes this.
  8. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,770
    Thanks @teutonicus for posting that! I'll look into that. I'm a little bit worried though that I might be heading down another rabbit hole though :( I'm sadly running out of time.
    I'm really looking forward to moving to something else, so 'PlayablesAPI' is on my radar for sure. I'd make the leap now but I'm using a lot of mirrored clips in my game and unfortunately it doesn't support that.