playMaker

Author Topic: 2 questions regarding facing direction in 2D [SOLVED]  (Read 5427 times)

Fat Pug Studio

  • Beta Group
  • Hero Member
  • *
  • Posts: 1294
    • Fat Pug Studio
2 questions regarding facing direction in 2D [SOLVED]
« on: January 10, 2017, 07:06:19 AM »
1. I've got some paths that i'm using in Simple Waypoint System in 2D, and this is what i'm basically trying to do.



Ship in the upper part of the waypoint moves as set in transform, but i want it to flip sprite or change it's Y rotation when it starts moving right, like the one down. It uses Catmull Rom without locked positions, so it always faces forward. Changing the transform or flipping a sprite is not a problem, but i don't know what parameter should i listen as a condition.

I'm pretty bad with quaternions, vectors and other rotation related stuff, so i'd really appreciate some help, i'm sure it's simple :)

2. I've got a type of enemy that measures distance from the player, once it is in range, it gets player position in that moment (let's name it PlayerPosition), and speeds up towards PlayerPosition with Move Towards. However, i don't want the enemy to stop when it reaches PlayerLocation, i want him to continue moving until it gets off screen. That's the first part of the problem, how to get the direction of movement and continue moving along it. The second is that i want the enemy to rotate towards the PlayerPosition.

As i said, i'm not really proficient with rotations, i've been checking out unity forums, and i see that some of the solutions include quaternion lerping/slerping, but i'm not sure how it is used. I tried using vector3 rotate towards, but to no avail.


« Last Edit: January 26, 2017, 12:22:37 PM by krmko »
Available for Playmaker work

Fat Pug Studio

  • Beta Group
  • Hero Member
  • *
  • Posts: 1294
    • Fat Pug Studio
Re: 2 questions regarding facing direction in 2D
« Reply #1 on: January 11, 2017, 06:13:02 AM »
I kind of solved the part of the second problem by spawning an object on PlayerPosition location which moves on the negative X axis, so the enemy chasing it with Move Towards action never manages to reach it and thus stop moving. Since it remains on the exact Y point, but not on the exact X point, it's not quite as precise, but it does the job.

Though i would like to hear how can it be done the proper way, as described in the image.
« Last Edit: January 11, 2017, 09:21:16 AM by krmko »
Available for Playmaker work

Fat Pug Studio

  • Beta Group
  • Hero Member
  • *
  • Posts: 1294
    • Fat Pug Studio
Re: 2 questions regarding facing direction in 2D
« Reply #2 on: January 13, 2017, 02:32:03 AM »
I solved the rotation part with Smooth Look At 2D, never tried it before. It also keeps the object moving towards the rotation target, which is nice.

Now the only thing left to solve is how to make an object move towards a point in space and get past it, with and without rotation. Any ideas?
Available for Playmaker work

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: 2 questions regarding facing direction in 2D
« Reply #3 on: January 13, 2017, 03:45:45 AM »
Hi,

 have you tried the "Movetowards" action. set the FinishDistance to 0 so that it always catch up and not just stop if reaches the target,

 Bye,

 Jean

Fat Pug Studio

  • Beta Group
  • Hero Member
  • *
  • Posts: 1294
    • Fat Pug Studio
Re: 2 questions regarding facing direction in 2D
« Reply #4 on: January 13, 2017, 05:08:56 AM »
No, no, you don't get what i want, so i made some small gifs. Here's how it works with 0 set as finish distance.



Target marks the player position in the moment of storing variable. That's the point the enemy ship should move towards. BUT, i don't want it to stop, but to pass it and continue moving in the same direction until it leaves the screen.

I made a solution by moving the target, so the enemy ship never stops, and it leaves the screen.



But that is not good, when the player is all the way down or up the screen in the moment of storing its position, the movement of the target will alter the angle too much. I want it to work the way i imagined it, on a direction set by non-moving target.

As for trouble #1, with sprite flipping depending on the direction, i've got it solved, it's supported in the Simple Waypoint System itself, i was just using wrong path type.
« Last Edit: January 13, 2017, 02:05:10 PM by krmko »
Available for Playmaker work

Chizzler

  • Junior Playmaker
  • **
  • Posts: 67
Re: 2 questions regarding facing direction in 2D
« Reply #5 on: January 15, 2017, 11:15:34 AM »
I made a solution by moving the target, so the enemy ship never stops, and it leaves the screen.

But that is not good, when the player is all the way down or up the screen in the moment of storing its position, the movement of the target will alter the angle too much. I want it to work the way i imagined it, on a direction set by non-moving target.

That's only an issue if you're moving the target in a specific direction. Imagine a line from the ship, going through the target and continuing off the map. You can move the target along this line to keep the relative direction.



In this example, your ship(S) is at position (2,2)  and the target(T) is at (4,5). Now you can work out the direction(D) using the formula D=T-S  (Direction = Target - Ship)
4-2 = 2
5-2 = 3
This gives you the direction of (2,3) 2 across the X axis, 3 across the Y axis.
So if you added (2,3)  to your target vector (4,5) you'd have a target of (6,8) highlighted in blue.

You'll notice that if you were to run that formula again with the new target position relative to your ship, it's doubled (4,6).  As long as you move the target in increments of your direction Vector, it'll always be the same direction relative to the ship.

SO once you've got your Direction (2,3)  multiply it by 100 (200,300)  and add that to your target position. It'll still be along that invisible line, so the angle wont change, but the target has moved further away.

I'm sure there's a more sophisticated way to do this, but I can't think of 1 that doesn't use 2dPhysics at the moment.
« Last Edit: January 15, 2017, 11:20:03 AM by Chizzler »

Fat Pug Studio

  • Beta Group
  • Hero Member
  • *
  • Posts: 1294
    • Fat Pug Studio
Re: 2 questions regarding facing direction in 2D
« Reply #6 on: January 15, 2017, 03:06:01 PM »
I think that way is more than good for what i need.  I'll see if i know how to do it.
Available for Playmaker work

Fat Pug Studio

  • Beta Group
  • Hero Member
  • *
  • Posts: 1294
    • Fat Pug Studio
Re: 2 questions regarding facing direction in 2D
« Reply #7 on: January 26, 2017, 08:02:53 AM »
@Chizzler

haven't got to this yet as i started working on some other stuff, but for the sake of saving some time. Do i get the direction by getting the vector of a ship with GetVector3, then vector of ships target with GetVector3 again, and then i use the Vector3 Operator Advanced to subtract target vector from ship vector and apply the result to ship vector?
Available for Playmaker work

Chizzler

  • Junior Playmaker
  • **
  • Posts: 67
Re: 2 questions regarding facing direction in 2D
« Reply #8 on: January 26, 2017, 11:46:49 AM »
"Get Position" on both the Ship and the Target. Storing them as Vector3

Subtract the Ship Vector from Target Vector using "Vector3 Operator" storing it as a new variable.
Now do a large multiplication to your new variable (To ensure the target position will move off screen) using "Vector 3 Operator".

Finally, "Vector 3 Add" your New Vector to your Target Vector and start your movement commands on your ship ("Move To" should work fine).

What we're doing is moving the target further away before movement even takes place. These calculations just make sure that your ship will path through the intended point on the way to it's new target position.

It's not a pretty solution, and it has limitations but it should accomplish what you're after. I'd recommend looking at the Physics actions for a more sophisticated solution, but i don't have much experience with that yet.

Fat Pug Studio

  • Beta Group
  • Hero Member
  • *
  • Posts: 1294
    • Fat Pug Studio
Re: 2 questions regarding facing direction in 2D
« Reply #9 on: January 26, 2017, 12:22:19 PM »
Thank you, i really got the grasp on coding since i started using Playmaker, but the transform stuff still manages to get the best of me.

I think it's a great solution, more than enough for me at this moment.

Thanks once again!
Available for Playmaker work

Fat Pug Studio

  • Beta Group
  • Hero Member
  • *
  • Posts: 1294
    • Fat Pug Studio
Re: 2 questions regarding facing direction in 2D [SOLVED]
« Reply #10 on: January 26, 2017, 12:54:51 PM »
I did some googling, there's a get forward action (i'm on my mobile right now so i can't check it out), and according to unity docs that's the thing that should be used, as it gets the direction the object is facing to. When i rotate the ship towards the target and get the forward, i should be able to make it move in that direction, i'll try that out too.
Available for Playmaker work

Fat Pug Studio

  • Beta Group
  • Hero Member
  • *
  • Posts: 1294
    • Fat Pug Studio
Re: 2 questions regarding facing direction in 2D
« Reply #11 on: July 01, 2017, 04:23:51 PM »
"Get Position" on both the Ship and the Target. Storing them as Vector3

Subtract the Ship Vector from Target Vector using "Vector3 Operator" storing it as a new variable.
Now do a large multiplication to your new variable (To ensure the target position will move off screen) using "Vector 3 Operator".

Finally, "Vector 3 Add" your New Vector to your Target Vector and start your movement commands on your ship ("Move To" should work fine).

What we're doing is moving the target further away before movement even takes place. These calculations just make sure that your ship will path through the intended point on the way to it's new target position.

It's not a pretty solution, and it has limitations but it should accomplish what you're after. I'd recommend looking at the Physics actions for a more sophisticated solution, but i don't have much experience with that yet.

Just wanted to let you know it works perfectly, thanks again!  ;D
Available for Playmaker work