I am trying to use the 2D portion of Unity3D for the first time so I am creating a breakout clone.
My issue is that the state management is getting decentralized... and I am not 100% certain if it is because a) It has to be, or more likely b) I'm doing something "wrong".
I have 4 major game objects, The ball, the paddle, walls, and blocks. The blocks state is simple, it starts at State N and each time the ball collides with it it moves to state N-1, at state N=0 it gets destroyed.
The walls and the paddle are also simple as they have only a single state each.
The ball is where it is getting tricky..
The ball has three states: Setup, LockedToPaddle and Moving.
It has a public function setState(state) :It's only public so the animation event can see it. SetState only changes to the new state if the new state is a valid state to transition to (For example, attempting to setState(Moving) from setup will not do anything, nor will setState(x) from state x.
When a state is successfully changed, it will also update the state variable in the animator.
The animator has an integer parameter State: 1=Setup, 2=lockedToPaddle,3=Moving
Each state has it's own animation, at the start of each animation there is an animation event which calls setState(State).
The setup state has a transition with a 3 loop exit condition, which moves to LockedToPaddleState
LockedPaddleState has a conditional transition on State=3 to transition to Moving.
Moving has a conditional transition on State=1 to Setup.
Further, I have an inputmanager game object with a delegate OnBallFire which triggers when the user presses the space bar. The ball gameobject has a method ball_OnBallFire() which calls setState(Moving)
Then In the collision2d method for the ball, if the ball collides with the "deadzone" (the area below the paddle). It calls setState(Setup).
As you can see... stateTransitions are made from all over the place... is this normal? Is there a way to centralize the state transition logic? Is it an issue that the animator can update the state, but the state can also update the animator?