playMaker

Author Topic: Flip World for 2D Platformer?  (Read 3099 times)

pootiki

  • Playmaker Newbie
  • *
  • Posts: 2
Flip World for 2D Platformer?
« on: April 13, 2014, 01:32:14 PM »
Hi there,

Quick question about flipping sprites in a 2D game based on the left/right direction.

In the recent Unity examples (if I understand them correctly), they use a "Flip" function combined with the horizontal axis input and a facingRight bool to flip the player sprite (or perhaps the entire world... I'm unsure) based on their current direction, and I'm just struggling a little to convert this into a Playmaker FSM. I realise I could just use their existing script and modify it as necessary, but, simply as a learning experience and to try to keep everything in Playmaker, I thought I'd ask for a little help on here. So, there are two parts to their script:

Code: [Select]
// If the input is moving the player right and the player is facing left...
if(h > 0 && !facingRight)
// ... flip the player.
Flip();
// Otherwise if the input is moving the player left and the player is facing right...
else if(h < 0 && facingRight)
// ... flip the player.
Flip();

AND

Code: [Select]
void Flip ()
{
// Switch the way the player is labelled as facing.
facingRight = !facingRight;

// Multiply the player's x local scale by -1.
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}

I'm fine with the first section, using a Float Compare and a Bool Test (although feel free to tell me if there's a better way of doing it), but I can't quite figure out the Playmaker actions and state arrangements that would be best to use for the second part, specifically, just the "Multiply the player's x local scale by -1" part.

Any advice?

It sounds like this guy has nailed it already, but he doesn't appear to explain how: http://hutonggames.com/playmakerforum/index.php?topic=6302.msg30613#msg30613

P.S. I'd also like to take this opportunity to say how amazing Playmaker is! Good job, guys.

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Flip World for 2D Platformer?
« Reply #1 on: April 14, 2014, 08:26:01 AM »
Hi,

 Check the Unity 2d platform example, it's all in there.

https://www.assetstore.unity3d.com/#/content/11228

bye,

 Jean

pootiki

  • Playmaker Newbie
  • *
  • Posts: 2
Re: Flip World for 2D Platformer?
« Reply #2 on: April 14, 2014, 11:53:43 AM »
Thanks for your reply, but I already have that example - that's where the code I supplied came from - but of course that doesn't show how the Playmaker FSMs should be set up.

So, my question is really what's the best way to lay out the following code as an FSM, and what actions are needed?

Code: [Select]
// Multiply the player's x local scale by -1.
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;

It's the "transform.localScale" I can't seem to find in the actions. I can scale gameobjects and things, but I don't know how to do it to the entire world. I believe that code affects the entire world anyway, rather than just the sprite in question. At least, that's what Unity help videos suggest. Perhaps I'm just being blind/stupid!

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Flip World for 2D Platformer?
« Reply #3 on: April 15, 2014, 10:22:41 AM »
Hi,

 don't worry about that, simply set the scale as normal. You have the rest of the logic right?

listen the the horizontal axis: if it's less than 0, then face left, move to a state where you check for the axis greater than 0 if so face right and move to initial state ( where it checkes for the axis being less than 0).

Bye,

 Jean