Playmaker Forum

PlayMaker Help & Tips => PlayMaker Help => Topic started by: RedOni on June 12, 2017, 01:21:46 PM

Title: Using an object's direction only and not it's position?
Post by: RedOni on June 12, 2017, 01:21:46 PM
I'm not sure if that was clear enough, so let me explain.

I have a 2D side-scroller set up with a crosshair (tied to the mouse) for the player to aim projectiles with. The problem is, it's using a ITween Move To with a Vector3, so the crosshair determines where EXACTLY it'll go and how fast it'll get there. I want my projectiles to travel at a set speed, and only travel in the direction that it's aimed at, not the exact position of the crosshair. Any ideas how I'd set that up?
Title: Re: Using an object's direction only and not it's position?
Post by: jeanfabre on June 13, 2017, 01:33:33 AM
Hi,

 you can determine a direction vector by substracting the target with the player position, this will give you the direction.

then instead of using iTween, I would simply use translate action like so:

-- normalize the direction ( so that the direction magnitude is 1)
-- instantiate your projectile at the player position ( or gun position)
-- translate your projectile of the direction * Speed

Does that make sense?

 Bye,

 Jean
Title: Re: Using an object's direction only and not it's position?
Post by: RedOni on June 13, 2017, 07:26:53 PM
Hi,

 you can determine a direction vector by substracting the target with the player position, this will give you the direction.

then instead of using iTween, I would simply use translate action like so:

-- normalize the direction ( so that the direction magnitude is 1)
-- instantiate your projectile at the player position ( or gun position)
-- translate your projectile of the direction * Speed

Does that make sense?

 Bye,

 Jean
They won't travel towards the crosshair now. They seem to want to move towards one fixed point in the scene, despite the fact that the Vector3 is constantly updating.

EDIT: Nevermind, it's almost working. I don't see how to add speed to the "Translate" action though, there's no option for it.
Title: Re: Using an object's direction only and not it's position?
Post by: jeanfabre on June 14, 2017, 04:40:04 AM
Hi,

 The "speed" for translation is how much you move on frame or per seconds ( which is better for consistency across devices), and so as I mentioned in the post above, to translate an object at a given speed, you need:

 The direction vector ( normalized, so that it's magnitude is 1), multiplied by the speed factor ( a float that you define in an fsm variable).

If you need a quick sample let me know.

 Bye,

 Jean
Title: Re: Using an object's direction only and not it's position?
Post by: RedOni on June 19, 2017, 12:27:36 PM
Hi,

 The "speed" for translation is how much you move on frame or per seconds ( which is better for consistency across devices), and so as I mentioned in the post above, to translate an object at a given speed, you need:

 The direction vector ( normalized, so that it's magnitude is 1), multiplied by the speed factor ( a float that you define in an fsm variable).

If you need a quick sample let me know.

 Bye,

 Jean
That would be helpful, thank you.
Title: Re: Using an object's direction only and not it's position?
Post by: tcmeric on June 20, 2017, 12:33:34 AM
I wrote a handy custom action. You can find it at the end of this thread. http://hutonggames.com/playmakerforum/index.php?topic=15212.msg70397#msg70397

You give it the start position and the target. Check the box "normalize", and you can get the normalized direction.

Just take that normalized direction and multiply this variable (normalize direction) by your desired speed.