playMaker

Author Topic: 2d sprite rotation / direction  (Read 1887 times)

4arrancho

  • Playmaker Newbie
  • *
  • Posts: 27
2d sprite rotation / direction
« on: January 14, 2019, 10:03:15 PM »
Hi

I'm trying to rotate a 2d sprite so it "looks forward" as we move it

i have get axis horizontal and get axis vertical
then i have "set velocity 2d" using these 2 axis
After i "get velocity 2d" and store it in vector2
Then "Get atan 2 from vector2"
and finally i have "set rotation" with the atan2 float in the z angle

It works great when i have the keys down but when the values from axis are 0 (keys up), the atan2 is also 0 and the rotation goes back to the original state

How can i get the sprite to mantain the last angle when the key is up?

I've tried other actions like smooth look and many others but i can't get them to work in 2d

Any tips?

Thanks

ch1ky3n

  • Full Member
  • ***
  • Posts: 208
Re: 2d sprite rotation / direction
« Reply #1 on: January 15, 2019, 01:43:01 AM »
Have you try to use float comparison to monitor the axis value?

if the axis drop below the certain threshold, move to the next state to set rotation based on last rotation (Atam2?), thus it will maintain its last rotation without being manipulated by the gradual drop of the axis

Best of Luck!

EDIT: you could also monitor the velocity drop as well if you are having a hard time with the axis.
« Last Edit: January 15, 2019, 01:48:52 AM by ch1ky3n »

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7615
    • jinxtergames
Re: 2d sprite rotation / direction
« Reply #2 on: January 15, 2019, 08:44:41 AM »
hi.
just thinking out of my head,
Maybe you can have a 2nd fsm.

On that fsm use a 'Get fsm int' and get the axis on the 1st fsm.
Then use an int compare to check if value is '0' or not (you can also get "Fsm Int Compare" on the Ecosystem instread)

if value is not 0, get the rotation value by using get fsm vector2 or vector3 depending on your needs.
Then make a state with a next frame event and loop back (if 0 AND if not 0)

Now you can get the value from the 2nd fsm to set the rotation on the object.

As i said i did not test this :D

4arrancho

  • Playmaker Newbie
  • *
  • Posts: 27
Re: 2d sprite rotation / direction
« Reply #3 on: January 15, 2019, 09:28:17 AM »
I already had those ideas but i was looking for something like 3d method

In 3d it's very easy like in this tutorial:

If i don't find other solution i'll try the int compare method...

Thanks

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7615
    • jinxtergames
Re: 2d sprite rotation / direction
« Reply #4 on: January 15, 2019, 09:39:03 AM »

4arrancho

  • Playmaker Newbie
  • *
  • Posts: 27
Re: 2d sprite rotation / direction
« Reply #5 on: January 15, 2019, 01:38:48 PM »
I made another fsm just to control rotation

It compares vector2 from velocity to zero and rotates or not.
It only works when i check the "late update" in the "set rotation" action.

Thanks to ch1ky3n and djaydino.

I didn't try the serenefox tip yet.