Im pretty new to game dev in Unity, and thought I'd start off with a relatively simple game:

Im planning to make a game where 3 cups are shown on a table, and a ball is placed beneath one, the cups go down (concealing the ball) and move around. If the player selects the right cup, they win.

Not sure if I'm jumping in the deep end, but I'd like to make this dynamic in the sense that the animation shouldn't dictate the place of the cup on the table, as I want to code it so that the amount of cups can be set (as a difficulty).

After looking into the state machine stuff behind Unity's AnimationController but not sure how I should go about it.

I've identified various states as below:

  • A cup can be down on the table
  • A cup can be lifted
  • A cup can be moved to a different position on the table (?)

You can see from the above that I dont know if I should include that as a state. When moving 2 cups, one cup should move forward to it's new position, and the other should move behind it as not to cause them to collide, but would also like to be able to code this in a way where cups on the far ends can be swapped as well.

Any advice?


2 Answers 2


This may be a little advanced for someone who is just starting out since what you're looking for is both procedural and involves avoidance, but it is definitely doable and will teach a lot if you go through with it.

Someone else may be able to come up with an easier solution, the following may be a good starting point.

1) Ditch the animator altogether.

Since you want to have a dynamic number of cups, the animator probably wouldn't work for you, at least not without lots of hacking.

2) Come up with an algorithm to decide on where the cups should go

You can consider the movement of the cups to be in steps. A complete shuffle may consist of n number of steps. In each step, you can do the following:

  • Assuming you have 3 cups in total, you have 3 possible slots for the first cup.
  • Pick a slot randomly, and store that as the target position for the cup.
  • Move on to the next cup, and pick a slot randomly from the remaining slots.
  • Continue until you have decided on a new position for each cup.

If you this step many times, you get a complete shuffle.

3) Avoidance

At each of our steps, after each cup knows its new target position, we would have to actually move the cups to their target position. We should do this in code. However, if we move each cup directly to their target positions, the cups would start clipping through each other.

If you're mathematically inclined, you could go into complex calculations of parabolas to determine the path for each cup such that there are no collisions. This, however, is not easy. A much simpler approach would be to find a way for each cup to avoid all the other cups as if it were sentient. I like to use steering.

With all that done, all you have to do is to fill in the rest of the code that makes the game playable.

Keep in mind, this is not the only way to do it. There are plenty of ways to create the same feature when writing a game. So don't worry about the details, start messing around with it and try getting something to work. Best of luck!

  • \$\begingroup\$ Cheers, that's pretty much what I had concluded myself after chatting to some Unity devs over this weekend. Will start hacking at it this week :) \$\endgroup\$
    – jarodsmk
    Sep 30, 2018 at 17:13

If your aim is to control the transitions on the Animator component via code, a simple way to use the Animator and have control via code is to:

  1. Create a parameter (in the Animator Parameters tab), say called CupState.
  2. Add a condition on the transitions concerning CupState.
  3. Change CupState values via code like this:

    MyAnimatorInstance.SetInteger("CupState", newState);

There are many ways to manage the controller via code (e.g. using Behaviours) but this is a simple way to start.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .