playMaker

Author Topic: How to follow object and update scale (2D)  (Read 2309 times)

Ardhan

  • Playmaker Newbie
  • *
  • Posts: 2
How to follow object and update scale (2D)
« 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.

RAWilco

  • Playmaker Newbie
  • *
  • Posts: 20
    • StinBeard
Re: How to follow object and update scale (2D)
« Reply #1 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).


- Create an Empty Game Object (or a cube, doesn’t really matter).


- Rotate the Empty Game Object/Cube so that the blue Z arrow points in the direction that your Chaser sprite is facing.


- Parent your chaser sprite to the Empty Game Object/Cube.


- Attach two FSMs to the Empty Game Object/Cube:
One for Movement


And one for looking in the Player’s direction

- 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.


- 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).


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.

RAWilco

  • Playmaker Newbie
  • *
  • Posts: 20
    • StinBeard
Re: How to follow object and update scale (2D)
« Reply #2 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.

Ardhan

  • Playmaker Newbie
  • *
  • Posts: 2
Re: How to follow object and update scale (2D)
« Reply #3 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.