Handling different animations for different weapons

Handling different animations for different weapons

PostPosted by JJC1138 » Wed May 09, 2012 8:11 pm

Hello.

In my game the player sometimes has no weapon, sometimes has a handheld weapon like a pistol, and sometimes has a shoulder-mounted weapon like a rocket launcher. Since they are held differently I have different movement animations (run, run backwards, strafe left and right) for each type of weapon.

I'm struggling to figure out what's the neatest way of setting up my Sage library to support these different weapon types. So far the only solution I can think of would be to have separate state machines for each weapon type, and duplicate all the setup in each of them. Is there a better way?
JJC1138
 
Posts: 5
Joined: Wed May 09, 2012 7:56 pm

Re: Handling different animations for different weapons

PostPosted by AlteredReality » Thu May 10, 2012 10:25 pm

Currently, there is no way I can think of using just Sage, other than the way you suggested. You would have to create State Machines for each weapon animation set.

However, you might be able to dynamically swap out the animation clips in the Animation object when the weapon is changed. So if you have animations such as "WeaponMoveForward", "WeaponMoveRight", etc, you could dynamically push other animation clips into those anim clips on the Animation object. You would do this using AddClip and RemoveClip. That way you could just have a state machine for moving and firing with a weapon that uses base weapon anim clip names.

Does that help at all?
Andy - Altered Reality
AlteredReality
Site Admin
 
Posts: 118
Joined: Fri Jan 06, 2012 8:31 pm

Re: Handling different animations for different weapons

PostPosted by JJC1138 » Thu May 10, 2012 11:12 pm

Thanks for the reply. I actually tried that very solution earlier today! Unfortunately Sage doesn't seem to like having the clips it's using being removed from the Animation component, even if they're replaced by other clips with the same name. As soon as I've done the swap I get errors of the form:

[Sage] Trying to set an anim clip weight on an anim clip that doesn't exist, "idle"!
UnityEngine.Debug:LogError(Object)
SageInternal.SageAnimationClipNodeSlot:SetAnimState(Single, Single, Int32, AnimationBlendMode, WrapMode, Boolean)
SageInternal.SageAnimationClipNodeSlot:Update(Single)
SageInternal.SageBaseNode:Update(Single)
SageInternal.SageBaseGraph:Update(Single)
SageInternal.SageBlendGraph:Update(Single)
SageInternal.SageBaseLibrary:Update(Single)
SageController:Update()


and the animation stops updating. I guess that makes sense since I presume the SageController caches references to the AnimationState objects rather than referring to the clips by name all the time.

I was hoping there might be a way of getting the controller to re-initialize and grab the new AnimationStates from the Animation. I tried stopping all state machines before swapping and restarting them afterwards, and I tried setting the library asset and the animation target of the controller to null, and then resetting them afterwards, but neither of those had the desired effect. I even tried destroying the SageController component and then recreating it afterwards, but that wasn't any good because the controller couldn't initialize properly without a library asset and as far I can see there isn't a way of passing one in before the new component does its Awake().

Is there some way of triggering the controller to re-initialize and grab the AnimationStates by name again? If not, I guess this is a feature request. :-)
JJC1138
 
Posts: 5
Joined: Wed May 09, 2012 7:56 pm

Re: Handling different animations for different weapons

PostPosted by AlteredReality » Thu May 10, 2012 11:31 pm

I actually just started fully caching all of those on initialize in 1.5.1. So it could be an issue with that. If that's the case, I would have to expose a way to notify that the clips need to be reloaded. Are you using Sage 1.5.1?

Although, the error you are seeing only gets thrown if the anim as not able to be cached, so maybe something is going wrong with AddClip and RemoveClip. Or at least it doesn't allow them to be accessible as Sage expects. What is is trying to access though, is the AnimationStateobject. Maybe that isn't setup on the Animation object after adding a new clip?

Regardless of that though, there is no way to get Sage to recache those AnimationClip and AnimationState objects if something changes. I'll add something to SageController to enable that for the next version. But recreating the SageController would definately do it. You just need to make sure the member variable of "animationTarget" is set. Even if awake has been called, you could simply call it again as it is public.

I'll try and do some more research at some point into adding and removing clips dynamically. It may very well need something else to make sure AnimationStates for those clips are added as well.

Let me know if you find out anything else as well.
Andy - Altered Reality
AlteredReality
Site Admin
 
Posts: 118
Joined: Fri Jan 06, 2012 8:31 pm

Re: Handling different animations for different weapons

PostPosted by JJC1138 » Fri May 11, 2012 12:31 am

AlteredReality wrote:I actually just started fully caching all of those on initialize in 1.5.1. So it could be an issue with that. If that's the case, I would have to expose a way to notify that the clips need to be reloaded. Are you using Sage 1.5.1?


Ah yes, I am using 1.5.1. I just tried reverting to the version I was using before (1.4.2 I believe) and that fixed the errors and everything is working as expected, so this is definitely to do with the new caching.

I'll have another go later today at getting this to work with 1.5.1 by recreating the component and report back.

Thanks again.
JJC1138
 
Posts: 5
Joined: Wed May 09, 2012 7:56 pm

Re: Handling different animations for different weapons

PostPosted by AlteredReality » Fri May 11, 2012 8:21 am

Okay, good to know the last version works. You could also use 1.5.0 though, which adds all the other new features such as nested graphs. If you don't have a copy of 1.5.0, email me at AlteredRealityEnt@yahoo.com and I can get it to you. Just send me your invoice number if purchased from the Unity Asset Store. If you bought it from our website, sending me the purchasing email is fine as well.

I'll work on implementing a fix for this ASAP.
Andy - Altered Reality
AlteredReality
Site Admin
 
Posts: 118
Joined: Fri Jan 06, 2012 8:31 pm

Re: Handling different animations for different weapons

PostPosted by JJC1138 » Fri May 11, 2012 11:27 pm

AlteredReality wrote:But recreating the SageController would definately do it. You just need to make sure the member variable of "animationTarget" is set. Even if awake has been called, you could simply call it again as it is public.

Hello. I had another attempt at this and wasn't able to get recreating the component working. I firstly tried just destroying the SageController component and re-adding one and setting its libraryAsset and animationTarget immediately after adding, but I get the error:

[Sage] Failed to create library, no asset set!
UnityEngine.Debug:LogError(Object)
SageController:Awake()
UnityEngine.GameObject:AddComponent()
<Swap>c__Iterator2:MoveNext() (at Assets/Scripts/AnimationPrefixSwapper.cs:59)

and then errors like:

[Sage] Trying to call SetFloat on a Sage Controller with no library!
UnityEngine.Debug:LogError(Object)
SageController:SetFloat(String, Single, Boolean)
SageController:SetFloat(String, Single)
PersonControl:Update() (at Assets/Scripts/Person/PersonControl.cs:66)

every frame. According to the inspector the library asset has been set by then, but I guess it misses the initialization in Awake().

I thought I might be able to work around that by making a prefab with the SageController with its library already set and instantiating that in as a child of my GameObject when needed. That failed with:

[Sage] The game object this Sage Controller is attached to has no animation. It must have an animation object in order to function propery!
UnityEngine.Debug:LogError(Object)
SageController:Awake()
UnityEngine.Object:Instantiate(Object)
AnimationPrefixSwapper:Awake() (at Assets/Scripts/AnimationPrefixSwapper.cs:20)
UnityEngine.Object:Instantiate(Object, Vector3, Quaternion)
<PlayLocally>c__Iterator6:MoveNext() (at Assets/Scripts/NetworkConnector.cs:82)

so I tried adding a dummy Animation component on the prefab as well to satisfy the initalization so that I could then reassign the animationTarget to the real target. That doesn't give any errors, but reassigning the animationTarget at runtime doesn't seem to have any effect. I can verify that it's set to the correct object in the inspector, but the new target's animation doesn't change at all. I also set up a quick test and tried reassigning the animationTarget at runtime in more normal circumstances (without swapping any animations) and that also seemed to have no effect.

Anyway, the upshot is that I couldn't find a valid workaround other than downgrading, so a function to signal a SageController to reinitialize its cached stuff would be very welcome. Thanks again!
JJC1138
 
Posts: 5
Joined: Wed May 09, 2012 7:56 pm

Re: Handling different animations for different weapons

PostPosted by AlteredReality » Sat May 12, 2012 10:53 am

Did you try calling Awake() manually (a second time) after the SageController has been created? Regardless though, I will definitely add something to handle this case of needing to recache the animations. This simply wasn't a test case I had thought of when testing Sage. So I will definitely do what I need to do in order to make this use case compatible. :)
Andy - Altered Reality
AlteredReality
Site Admin
 
Posts: 118
Joined: Fri Jan 06, 2012 8:31 pm

Re: Handling different animations for different weapons

PostPosted by JJC1138 » Mon May 14, 2012 3:18 pm

AlteredReality wrote:Did you try calling Awake() manually (a second time) after the SageController has been created?

Ah, that's a great idea! That works fine in 1.5.1 (even without needing recreating the SageController at all). I feel a bit silly for that messing about with prefabs and such now. :-) Thanks!
JJC1138
 
Posts: 5
Joined: Wed May 09, 2012 7:56 pm

Re: Handling different animations for different weapons

PostPosted by AlteredReality » Mon May 14, 2012 3:54 pm

Good to know it works. Although 1.5.2 should be released soon (waiting for approval now), and then you'll just call RecacheAnimations() on the controller. :)
Andy - Altered Reality
AlteredReality
Site Admin
 
Posts: 118
Joined: Fri Jan 06, 2012 8:31 pm

Next

Return to Questions and Discussion

Who is online

Users browsing this forum: No registered users and 1 guest


Style by Gokinstudio | Free forum hosting by ProphpBB | Software by phpBB | Report Abuse | Privacy
cron