Playmaker Forum

PlayMaker Help & Tips => PlayMaker Help => Topic started by: Ardhan on October 05, 2014, 11:00:48 AM

Title: How to follow object and update scale (2D)
Post by: Ardhan on October 05, 2014, 11:00:48 AM
Hello. This is 2D related (working with 2D toolkit sprites).
I want to make an enemy follow the player. This is easy, with Move Towards action.
BUT, when the enemy faces left, I want to set its X scale to -1, to mirror it.
How can I update its X scale to mirror the sprite ?
I hope someone can tell me how to do this right.

My approach is FAR too complex:
I tried to do it with two FSM. One handles movement, and the other handles  mirroring (with two global transitions, triggered by the movement-FSM).
But this way, events are sent every frame even if mirroring did not change.

The movement FSM compares X position with last frame's X (stored in xOld variable using a Get Position action as last state action). If x>xOld, we are looking right, and an event is sent to the mirroring-FSM. If x<xOld, looking left event is sent.

I tried to avoid calling mirroring-FSM every frame, by adding an aditional state in movement-FSM, but I got infinite looping (because I checked a bool variable to see if mirroring changed, and if it didn't, I went back to first state).

I'm an experienced programmer, but I just don't know how to use Playmaker with such a simple behaviour. I spent couple of hours before asking. I'd like to learn, so thanks in advance.
Title: Re: How to follow object and update scale (2D)
Post by: RAWilco on October 07, 2014, 03:43:40 PM
I’d tackle this problem like this:

- Here’s your two sprites (let’s say the green chap is the Player and the purple one is the Chaser).
(http://media.tumblr.com/8698c90bffbded76cb0d7d7e6864fbe9/tumblr_inline_nd3ad0xD5r1ra1sgq.png)

- Create an Empty Game Object (or a cube, doesn’t really matter).
(http://media.tumblr.com/a4e2a796f4362c0b54d993663dea9ccd/tumblr_inline_nd3ad64buh1ra1sgq.png)

- Rotate the Empty Game Object/Cube so that the blue Z arrow points in the direction that your Chaser sprite is facing.
(http://media.tumblr.com/12f6298b91d5022fb923dad9cb079490/tumblr_inline_nd3adeXthk1ra1sgq.png)

- Parent your chaser sprite to the Empty Game Object/Cube.
(http://media.tumblr.com/6e22f78726def9fd85c26eec6e2c7073/tumblr_inline_nd3adsAyUB1ra1sgq.png)

- Attach two FSMs to the Empty Game Object/Cube:
One for Movement
(http://media.tumblr.com/e779eab708b7550950af550b96c1cb82/tumblr_inline_nd3adyZZbC1ra1sgq.png)

And one for looking in the Player’s direction
(http://media.tumblr.com/f7b140869f80734ff5d98307f4c56462/tumblr_inline_nd3ae8jxPE1ra1sgq.png)
- Don’t forget to tick the “Keep Vertical” and “Every Frame” boxes to keep the sprite from rotating up and down and to make sure it keeps looking…well, for every frame.

- And if you make sure that both the Chaser’s parent and the Player have the same Z Coordinate, then the sprite should flip instantly to face toward the Player’s direction when it moves.
(http://media.tumblr.com/ee7b79ba91acae29cff67643ee1e008b/tumblr_inline_nd3aef81ph1ra1sgq.png)

- And if you use a cube like I did, don’t forget to delete the Mesh Renderer so that you can actually see the Sprite when it flips (don’t forget to get rid of the unnecessary box collider as well).
(http://media.tumblr.com/09f885b36aa183491286a68612e5c28b/tumblr_inline_nd3aeoQOCr1ra1sgq.png)

Sorry if any of this seems like I’m teaching you to suck eggs. This is just the way that I prefer to have things explained to me so that I know that I don’t miss anything. Anyway, I hope that makes sense. It's the cleanest way I can think of doing this: using just two FSMs, two actions and one empty game object.
Title: Re: How to follow object and update scale (2D)
Post by: RAWilco on October 08, 2014, 07:10:31 AM
In fact, now that I think about it, you would probably get away with using both of those actions in one FSM. I see no reason why they should conflict with one another.
Title: Re: How to follow object and update scale (2D)
Post by: Ardhan on October 10, 2014, 04:47:12 PM
Thanks for your detailed reply.
Indeed, it seems like a quite nice solution.

Anyway, I'm going to write my own custom action, built upon MoveTowards, but doing an automatic scale update, and some easing-in speed. Quite nice for 2D stuff.

But, If someone has any other suggestion, I'd appreciate it.
Thanks again.