playMaker

Author Topic: Face direction 2D side-scroller  (Read 2886 times)

Gojtan

  • Playmaker Newbie
  • *
  • Posts: 9
Face direction 2D side-scroller
« on: June 21, 2020, 06:25:48 PM »
Hi!

I've been trying to get my Player to face the direction, with 3D there's smooth look at direction, but in 2D I can't really find any good substitute for this.

I've been trying with get Vector, Get Axis and Get Velocity to turn left when going left and vice versa, but I can't get it to work.

Any ideas?

Cheers

Thore

  • Sr. Member
  • ****
  • Posts: 480
Re: Face direction 2D side-scroller
« Reply #1 on: June 21, 2020, 06:38:13 PM »
This is done by “flipping” the parent object that holds the sprites. Or, if its all one sprite, the sprite renderer has a flip tickbox. I don’t know if there’s an action for this, but you can achieve the effect by setting the scale of x to -1 or negative of whatever the scale value is. Alternatively, rotate the Y by 180 degrees.

To do this, use float compare on every frame and set scale once (not every frame). In one state you check if input is smaller than -0.1 then go to flipped, and in that state check if input is greater than 0.1 then go back to the other state, and flip back. This makes it so that the character also stays in that last orientation when input is 0.

Gojtan

  • Playmaker Newbie
  • *
  • Posts: 9
Re: Face direction 2D side-scroller
« Reply #2 on: June 21, 2020, 06:56:44 PM »
This is done by “flipping” the parent object that holds the sprites. Or, if its all one sprite, the sprite renderer has a flip tickbox. I don’t know if there’s an action for this, but you can achieve the effect by setting the scale of x to -1 or negative of whatever the scale value is. Alternatively, rotate the Y by 180 degrees.

To do this, use float compare on every frame and set scale once (not every frame). In one state you check if input is smaller than -0.1 then go to flipped, and in that state check if input is greater than 0.1 then go back to the other state, and flip back. This makes it so that the character also stays in that last orientation when input is 0.

Cheers man, couldn't get it to work now, but I'm tired AF.

Hopefully I can get it to work from this tommorrow though, appreciate your help once again!

PS. It's all one sprite. It's litterally a potato with eyes.

Thore

  • Sr. Member
  • ****
  • Posts: 480
Re: Face direction 2D side-scroller
« Reply #3 on: June 21, 2020, 07:28:42 PM »
It’s pretty easy.

Make a state with two actions, 1) getting the input axis x (horizontal). And 2) float compare. Then copy paste this, and name one facing right, the other facing left. Now configure the float compares, and link them together with events.

You can then hit play, and you should see how the states correspond to the inputs. Of course nothing happens yet. If that works, you can set scale or rotate, or flip the bool etc.

Gojtan

  • Playmaker Newbie
  • *
  • Posts: 9
Re: Face direction 2D side-scroller
« Reply #4 on: June 22, 2020, 09:09:06 AM »
It’s pretty easy.

Make a state with two actions, 1) getting the input axis x (horizontal). And 2) float compare. Then copy paste this, and name one facing right, the other facing left. Now configure the float compares, and link them together with events.

You can then hit play, and you should see how the states correspond to the inputs. Of course nothing happens yet. If that works, you can set scale or rotate, or flip the bool etc.

What am I doing wrong? It works now, but when there's no input, it just turns right.

(It also turns left first time I hit right arrow, but then it turns correct way lol)





(Left & Right state is just turn-action)

Thore

  • Sr. Member
  • ****
  • Posts: 480
Re: Face direction 2D side-scroller
« Reply #5 on: June 22, 2020, 03:29:13 PM »
It’s not super clear as top and bottom now correspond to left and right, and the states left and right actually mean flip left or flip right. If you start facing right, then “right” should be the start state. It’s clearer with two states, arranged left and right.

Also, when you are in the left stare, you check for +0.1 go to facing right, and in the right state -0.1 to go to the left state, so that zero will not cause a state change.

Gojtan

  • Playmaker Newbie
  • *
  • Posts: 9
Re: Face direction 2D side-scroller
« Reply #6 on: June 23, 2020, 07:18:18 PM »
It’s not super clear as top and bottom now correspond to left and right, and the states left and right actually mean flip left or flip right. If you start facing right, then “right” should be the start state. It’s clearer with two states, arranged left and right.

Also, when you are in the left stare, you check for +0.1 go to facing right, and in the right state -0.1 to go to the left state, so that zero will not cause a state change.

Ahh. Makes sense actually.. lol.

Thanks a bunch!

Edit; I was overthinking alot, plus I missed that actually had a script enabled which messed things up aswell :3
« Last Edit: June 24, 2020, 03:33:13 PM by Gojtan »