Friends, food, and flourishing

Cathedral animals logic

This post is about implementing the behavior of the animals in the cathedral level. I wrote it to remind myself of the architecture, so I can reuse it on other levels.

Let’s start by looking at what the player sees.

Two animals

Each nugget has two animals, a main one the player interacts with, and a companion animal the main one controls.

A nugget has two animals

Here’s the interaction:

The animals are running idle animations continuously. There are several idles, and the animals never run the same one at the same time (it looks strange). If the main animal is running animation 1, the slave will run a different one. The animations could have different lengths.

When the player approaches the pair, each one will finish the animation it’s running. Then they’ll run an “attention” animation, like standing up. Then they’ll return to their earlier behavior.

How to do it

Here are the components for the main animal:
Main animal inspector
The flourishing interaction controller displays the E-to-interact prompt to the player, and the flourishing content from the server. The same as for any nuggety object. No more on that here, since it’s the animations we’re interested in.
The animator has a controller customized for CK. Here it is:
Animator controller
Each node plays one idle animation, exiting when the animation is done. Here’s the transition from Ready to idle 3:
Transition from Ready to idle 3
The transition is tied to the variable iIdle. playing the animation when iIdle is set to 3. As you might guess, each idle transition has a different value for iIdle.
Here’s the transition back to Ready:
Idle 3 to ready transition
So, the algorithm is basically:
  • Choose an animation to play.
  • Play it.
  • When the animation is over, choose another one.
A script (CathedralAnimalAnimatorReadyEventResponder) on the Ready node handles switching between animations. Unity fires the OnStateEnter event when a state is entered. The code chooses the next animation, and sets iIdle to the right value to trigger it. The animation is chosen to not be the same as the previous value, or the same as the slave animal is using.
So far, so good. That will play the idle animations, one after the other, choosing randomly within constraints. But how to change things when the player interacts with the animal nugget? Remember, we want to finish the current animation, then play a special stand-up animation.
Remember iIdle? It’s used to trigger the animations, except for one, the stand-up:
Special stand animation node
It isn’t controlled by iIdle, but by another variable:
Entering the stand node
The player could press E to interact any time during an idle animation. We don’t want to interrupt the current idle animation, but let it finish, and then play the stand animation. So, the player interaction code does not change bInteract directly. Instead, it sets another flag that OnStateEnter checks the next time it runs. If the flag it set, OnStateEnter sets bInteract.
Here’s the hierarchy:
Cathedral1 is the main animal, and the nugget controller. Notice the child object for the collider controller.  The collider controller runs when the player enters a trigger collider that contains both animals:
Collider on the main animal
The main job of FlourishingCathedralBeastTriggerColliderController is to tell CathedralAnimalAnimatorReadyEventResponder (the class that owns OnStateEnter that runs when an idle animation is over) that the player pressed interact. When OnStateEnter next runs (that is, when the current idle animation ends), it sets bInteract to run the stand-up animation.

There’s more, but…

There’s more happening. For example, when the player presses the detect key, the nuggets (tied to animals) have to show they’re ready to interact.
Nugget detection
That’s enough for this post, though.