Forum rules - please read before posting.

Switching Animation Controller while playing

Hey!
Is it possible to switch the Animation Controller Adventure Creator is using for the player while playing?
Recently, I bought an asset ("2D Customizable Character") that uses 4 different directional Controllers for each character, separated in 4 child objects (which is important for it's script to reassemble the character's clothes in every direction).
By using the "Sprites Unity Complex" Animation engine, at the start of the game, the standard-direction Controller ("down") is linked to the Adventure Creator's Mecanim Animator parameter and I can play every animation that goes this direction. But when I go left (e. g.), I have no idea how to change the parameter to the Controller that includes all Animations for the direction "left" as well.

Looking forward to any ideas!
Stefan

Comments

  • edited March 2

    Hi Stefan

    I guess you mean the Daniel Thomas packs?

    I'm successfully using these in one of my own projects, but I ended up having to write a Custom Animation Controller in order to use them

    You can find a discussion of some of the issues involved: here

    As can be seen, Chris doesn't think that my Custom Controller approach was necessary, so it's quite probable that they could be animated within standard AC somehow

  • Welcome to the community, @inc0ming.

    Have a look to see if my instructions in @HarryOminous's link are valid for this too, otherwise we'll have to consider a possible script-based solution.

    I'm not familiar with the pack you're referring to - could you share some screenshots of the way it's set up, so that I can better understand how it's arranged and how you're currently connecting it to AC.

  • edited March 2

    Thank you and thanks for your help!
    Yes, I'm talking about the Daniel Thomas packs. I tried to move around the child objects but the character setup script of their parent is needed to update clothing and color etc., so it won't work. I was able to link the Adventure Creator's Animator parameter to the first child (which is Down). Which means, I can play every animation of the down-direction using Adventure Creator, but I can't switch direction.

    It would be great, if I could control everything from the first child like you suggested. I also tried playing around with the Animator controller and putting the directions together, they are not playable anymore if I do though. I guess that's because they would use the avatar that is directed by the parent object.

    I'll provide some screenshots for you.

    Maybe scripting is the only way?

    https://postimg.cc/w1T3LPYw

    https://postimg.cc/yJ2dq8gG

    https://postimg.cc/2bv5CNnS

  • I'm sure that once Chris sees how the characters are constructed he'll be able to come up with the optimum solution for how to get them animated

    My solution (which almost certainly is not optimum) was to keep DT's Up, Down, Left and Right animations as children of the main Player/NPC character (together with DT's Character SetUp script), and then to simply switch the appropriate GameObjects on/off in my Custom Animator, depending on which direction the character is facing (acChar.GetSpriteDirectionInt();)

    So, if the character is facing up, then I switch on the Up GameObject and its associated animations, and switch all the other ones off. (As an aside, I also use my Custom Animator to trigger the two supplied Attack animations, when required)

    This all seems to work fine so far in my small game world, with only a few characters running around, but I'd guess it might not be so great with a lot of characters on screen, and like I say, I'm sure there are better solutions to be found in any case

    Anyway, here's a quick screenshot of one of my Player character Prefabs, in case it might be of interest:

  • Thank you, that's helpful!

    I tried to follow your solution but writing a Custom Animation script seems to exceed my skill level. I was able to enable/disable the child objects depending on the direction of the char with C#. But I don't get Adventure Creator to use the Animation Controller of the activated child.

    So, if the player heads left, only the GameObject for left is switched on, but the animations of this GameObject would still not be used by AC.

  • If the way this animation asset is "supposed" to work involves multiple Animators, then isn't it designed for each child to be enabled/direction based on the character's direction anyway?

    If so, is there a function or variable within the "Female Character Setup" script to control the direction?

    I'm not privy to how the script works, but I imagine it would be possible to adapt it to only rely on one Animator - you may want to contact the asset's author for advice.

    However, if a separate Animator for each direction/child is necessary, what you could try is to attach a "dummy" Animator on the sprite-child, assign that as your character's Animator, and use that to transfer Animator parameter values to each of the children.

    Attach the Animator and give it a new controller with each of the same parameters (Speed, Direction, Angle etc) that you've already defined in the Player Inspector. You can then attach a simple script to each of your "directional" Animators that simply copies these values. So long as you only rely on parameters to control animation playback, they should sync up:

    http://pasteall.org/1525550/csharp

  • edited March 5

    I'm not sure that approach (copying the parameters) is going to work in this instance. Although each directional GameObject in the basic DT prefab does have its own Animator, it's a very basic affair that just includes the relevant animations

    There are no parameters included to fire them off. The CharacterSetUp script is really intended just for setting the characters up in the first place

    I'm sure you're right, that it should be possible to only have a single top-level Animator, but I never pursued that path in the end. I found that by overriding PlayIdle, PlayWalk, and PlayRun, in my CustomAnimEngine, to effectively just "Play" the requisite animation, in conjunction with enabling/disabling the relevant child GOs, I was able to achieve a working system (good enough for my meagre needs anyway)

    I'd share more of it here, but it relies on other stuff I've got in place, and it would be quite hard to disentangle just the relevant code

  • Thanks again. I also got a reply from Daniel Thomas:
    "What you can try, if you don't want to create a custom scripts, would be to add all animations you need to one animator(on parent for an example). So now the problem is it's not one 'character' that is animated, but each direction has it's own character setup. To solve this you could simply add keyframes to activate/disable the direction game objects. So for an example:

    • You would need to copy side animations you need and move one 'right' and one 'left'
    • In the moving,ex right, side animation you would disable up,down,left game objects and activate the right game object.
    • Repeat for all animations you need and all directions.

    This should, when done, leave you with one animator on the parent which have all the animations and the animations activate/disable the different directions.

    It's a bit of work, but this solution should work with AC without making custom scripts.
    Let me know if that worked out for you.

    Daniel Thomas"

    I'm not quite sure how this would work though. I did try relying on one Animator on the parent object but if I do, no animation is played at all. I had various approaches to solve the problem in the last days but none of them was successful yet. I'll let you know if I make any progress.

    Your solution sounds very interesting to me @HarryOminous . I might play around with custom scripting as well. I understand that disentangling the whole code is difficult and problematic but every help is appreciated.

  • I think I get what he's suggesting.

    Arrange each direction so that they all have the same parent, which itself is a child of the root, and set as the character's "Sprite child". Give this child an Animator, and within it create e.g. an Idle and Walk animation.

    Each of these animations would animate all directions simultaneously. So Walk, for example, would cause the downward, upward, rightward etc child sprites to all animate walking - and all within the same Animation Clip asset. Wire these animations up using the "Move speed float" parameter as normal.

    Then create a set of animations (one for each direction) that causes one directional child SpriteRenderer to be enabled, while all others disabled - so that each animation causes only one direction to show at a time. You can then place these in a sub-layer within your Animator, and hook them up to the AC character's "Direction integer" parameter so that the appropriate one is played according to which way the character is facing.

  • edited March 6

    @inc0ming I'd strongly suggest pursuing Chris/Daniel's suggestion, as it will likely result in the best possible solution to the problem

    ...

    However, if you get stuck, or just want to have a play around with the method that I'm using, then I've created a couple of "Lite" versions of the two scripts I use

    In order to keep things simple, these have been cut down so that they only work for the Idle and Walk/Run animations, not any of the fighting or death animations. I've tested them successfully in my own game (which uses AC's "Point and Click" Movement Method), but, yanno, computers and all, I can't guarantee they'll work in all other contexts

    http://www.pasteall.org/1529259/csharp

    http://www.pasteall.org/1529261/csharp

    To use...

    1) The scripts can be stored anywhere sensible in your project, and named "CustomAnimatorLite" and "CustomAnimEngineLite"

    2) Take one of DT's prefabs (including the Male Character Setup, or Female Character Setup scripts) as a starting point

    3) If it's to be used as a Player character, set its Tag to "Player"

    4) Add a Rigidbody2D, and a CircleCollider2D

    5) Add the "CustomAnimatorLite" script. In the Inspector, set the UpAnimTransform, DownAnimTransform, LeftAnimTransform and RightAnimTransform to point to the various appropriate underlying child GameObjects

    6) Add AC's "Paths" and "Moveable" scripts

    7) Add either AC's "Player", or "NPC" scripts, as appropriate

    8) Set AnimationEngine to "Custom", and ScriptName to "CustomAnimEngineLite"

    9) Save it as a new Prefab

    That should be enough to get a working character. You may then want to play around with some of the settings, such as Walk and Run speed

    P.S. One potential gotcha is that the two script names are rather similar. It's important to make sure that the correct name is specified in each instance when following the above instructions

  • Thank you for all your support!

    So here's an update:

    I linked the directional child objects to one parent object and managed to put the animations of, e.g., "Idle" in one clip all together. I linked each animation to it's child sprites and wrote a very basic script to enable/disable the directional game objects depending on the direction with direct movement.

    pasteall.org/1540653/csharp

    https://postimg.cc/dhSc5819

    If I preview the animation, everything works fine:
    https://postimg.cc/LYjd4wVy

    However, when I play the game, although AC is able to use the Speed float to switch from "Idle"- to "Walk"-state, no animation is played:
    https://postimg.cc/PLgsjT1p

    Idle works fine, but the char refuses to walk, no matter what I do.

    Here's a screenshot of how I set up the animations in the AC inspector:
    https://postimg.cc/v1gFkcxw

    Any ideas what went wrong?

  • If the "Walk" animation is highlighting in the Animator at the correct time, then AC is doing its part of the job. I don't know what the contents of your Walk animation are, but it may be an issue with the animation itself.

    However, setting the child object states through script isn't ideal - at least not by checking for input key. A character can move via a Cutsene, or be an NPC, and there'll be a separation betweem the input and their intended facing direction.

    Through script, you can get a character's facing direction with:

    myCharacter.GetSpriteDirectionInt ();

    (See the link for the return values)

    The cleanest way, though, would be to do without script - and again, just use a sub-layer of the Animator to animate the child object states according to the exposed "Direction integer" parameter in the character's Inspector.

  • Thank you. I followed your advice and arranged everything in a sub layer, which works perfectly fine.

    This feels really close to the best solution now.

    I also got a better understanding for the problem with switching the animations:
    if I walk in the same direction as the starting direction int, the walk animation is played. But as soon as the direction int changes, the walk animation cannot be played anymore, even if I go back the direction I started with. Does this sound like an AC problem to you or might this be an Unity-based issue?

    Thank you for all your support! This is incredibly helpful!

  • edited March 14

    if I walk in the same direction as the starting direction int, the walk animation is played. But as soon as the direction int changes, the walk animation cannot be played anymore, even if I go back the direction I started with. Does this sound like an AC problem to you or might this be an Unity-based issue?

    It depends on the state of the Animator window. With Sprites Unity Complex, AC will only control the parameters you allow it - so if those parameter values are correct, it's likely on the Unity/Animator side of things.

    Check that your animation transitions are correctly configured, so that the parameter values cause the correct states to play, and then see if the animation states themselves are correct.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.