x


Best practice for programmatic character animation

Hey all,

I'm having a bit of an architecture dilemma. I'm trying to decide how I should control the animation of my little robot character. Here's the scenario:

  1. user selects a tile for the robot to move to
  2. if the robot is not facing the right direction, rotate the body in the right direction
  3. move the robot to the destination tile

Where I'm having trouble is in sequencing the animations. Should I use a Coroutine to animate the rotation and then once complete start the move Coroutine? Or should I set up a state machine and just check if the robot is in the right state on update (Idle, Rotating, Moving)?

I feel like the state machine is probably the more traditional way to do it, but I wanted to get some other opinions.

Cheers

more ▼

asked Jul 15 '11 at 01:03 AM

meroon gravatar image

meroon
28 3 3 8

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

Well, if the task is not interruptable a coroutine would work fine. If you want to be able to stop the process at any time a state machine would be better ;). A coroutine is really useful when you have a quite linear command chain.

IEnumerator MoveToTile(Vector3 target)
{
    //Start looping rotation animation here
    while(not facing the the target)
    {
        //Rotate
        yield return null;
    }
    //switch to run / walk animation
    while(not reached target)
    {
        //Move forward
        yield return null;
    }
    //stop the animation / switch to idle...
}

Just in case you didn't know: coroutines are also statemachines but the state-handling is hidden. The compiler even creates a whole seperate desicated nested class for each coroutine. Since the state-stuff is not accessible, coroutines are not as flexible as own statemachines ;)

more ▼

answered Jul 15 '11 at 02:57 AM

Bunny83 gravatar image

Bunny83
45k 11 48 206

This worked great for me and made total sense for my specific problem. Cheers!!

Jul 15 '11 at 05:23 AM meroon
(comments are locked)
10|3000 characters needed characters left

I'd say that every Update event, the robot should calculate afresh whether it needs to turn or move forward. So a modeless solution. Only if that choice was expensive to make would I use a modal approach. The trouble with the modal approach is that you then need more code to kick it out of the mode (eg. when player chooses new tile before robot reaches first choice).

more ▼

answered Jul 15 '11 at 03:01 AM

Waz gravatar image

Waz
6.4k 22 33 70

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3768
x323
x9

asked: Jul 15 '11 at 01:03 AM

Seen: 1232 times

Last Updated: Jul 15 '11 at 06:20 AM