Hello all, I am currently working on a 2D game that I have divided the sprites of a 2D model into torso, and legs. This has worked as intended beautifully and created it to where characters can attack, reload, or “do nothing” all while the legs are either moving or not. Now that it works I want to incorporate a state machine to help with the logic, the issue I’m running into is that with the current way of handling the sprite animation logic one sprite only cares about the action, the other sprite only cares about moving. So with this 2 states need to be simultaneously true, i.e. idle & attacking, moving & reloading, etc. Due to moving being the most obvious and easy to manage I could just use moving as a boolean and then the actions as states?
I would make unique states for each animation segment. Do you by chance use an AnimationTree? Which has a state machine mechanism.
I have a dog character that has 4 different segments. Each segment has an AnimationTree and state machine node. My script right now is setting a body state, tail state, head state and eye state.
In my case I have an idle/walk/run animations with tail up and down. The tail needs to move different with each movement state, but has another state to be up or down. So in this case it’s leveraging two states to support every combinations of the state machine.
This tree handles the tail visual state. top three are tail up for each movement state, and the bottom three are for tail down. they transition up or down based on an excitement threshold that changes the tail state. and the movement state changes base on velocity that sets the body variable state.
var excitement : float = 0.25
var body : int = IDLE
var head : int = FORWARD
var tail : int = UP
func _physics_process(delta):
...
# body
if is_zero_approx(velocity.x):
body = IDLE
elif abs(velocity.x) < SPEED:
body = WALK
elif abs(velocity.x) >= SPEED:
body = RUN
# tail
if excitement >= 0.5:
tail = UP
else:
tail = DOWN
...
I update these values to trigger transitions to animations.
Very interesting I didn’t know you could do that with the Animation Tree node. I do believe this will solve it. I’m going to try tomorrow but if I’m understanding I would create an animation tree node on each segment, so in my case Torso and Legs, handle the logic of each in their respective tree, and then change the state in the player movement code.
I haven’t played with animation tree yet so I’d be interested to know how the relationship between a state node and the tree works. If I’m understanding your code correctly you’re not even using any sort of new_state() method but yet I assume you have the node handle the logic. So for example I assume you have an idle, walk, and run state node that handles whatever logic is needed but yet the animation tree can switch those nodes itself I assume?
Thanks for the tip I definitely am excited to play around with it =).
Each node you see in the graph represents an animation. The arrows are AnimationNodeStateMachineTransition types which look at the player script periodically and test the state with the a custom evaluation expression. tail == UP for example. When it sees that tail == UP evaluates to true it will traverse to a node where I set the animation with the tail up. That node will have potential exit Transition expressions to move to the next animation node.
In my case I’m in a fully connected graph where each animation node can transition to the next. It theoretically can’t get stuck, but just be aware about how code can change state and how the transitions are setup. My approach is setting up the script to represents the target state and the animation system follows.
( it can do a lot more through, it’s pretty powerful, this is just an example.) I will say that each of my AnimationTrees looks fairly similar and it is a little tricky to copy paste work.