Case Status
Log In


Home ยป Samples»M2H Catch Egg»M2H Catch Egg Game, User Input…
Community Wiki

M2H Catch Egg Game, User Input Fsm

Move the bucket left and right.

Also limit the bucket movement on the left and right.

the left and right bounds are exposed in the inspector for easy tweaking
This is simple Fsm, showing one possible technic to bound values.
As we start, we watch user input and multiply it by a speed variable ( exposed in the fsm component inspector for easy tweaking), and we translate the bucket by that amount
Now, the "catch" is we want to limit the bucket movement left and right, even when the user keep pressing left or right input. To do that, we simply watch the bucket position. 
We are still in the same state "move bucket", we just translated the bucket, now we get its position, store the x position into the variable "current position" and compare it with the "left bound" and "right bound" ( also exposed in the Fsm inspector for easy tweaking).  
If we detect that the bucket has reached the left or right bound, we trigger the appropriate event: for this, we created two Events, "LEFT LIMIT" and "RIGHT LIMIT" used as transition to states that simply constrain the bucket to that limit. Notice we go straight back to the "move bucket state" to make sure we keep watching for user inputs.
So practically, as the user keep pressing the right input, we move the bucket, further to the right, but detect it's too far, and constrain it, move it again too far, and again constraint it.
This could be seen as too much processing for nothing, the other solution would be to stop watching for input until the user release that input, but it would become a lot more "complicated" in terms of Fsm, states, actions and variables, so this solution works very well, while maybe not the most efficient, but the game being very simple, the extra over head of mving/constraining it totally unoticeable, even on mobile, and this case is unlikely permanent as the will keep moveing left and right to catch up eggs. One other solution if you want to experiment with this is to move the bucket with addforce and put two colliders on each side, then the bucket will collide against them and effectively not move under the force trying to push it further.

Last modified on 5/11/2012 2:13 AM by User.

  • RSS Feed