# How do I increase speed based on touch distance

0 Favourites
• 6 posts
• Hi guys im new to contruct 2 and im trying to create a player who is controlled via touch input, I would like its movement to increase based on touch position/distance (the further away the faster the player speed); for example if touch is at a distance of 100 px from the player then the player would move at speed of 10 to that position and decrease while reaching the touch position, if touch is at a distance of 200 px from the player then it wil move at a speed of 20 to that position and decrease while it gets closer to the touch position until it reaches a speed of 0 (position of player = position of touch).

my events at the moment are like this:

Touch> is in touch> Player > move 300*dt pixels at angle (self.X, self.Y, touch.X, Touch.Y)

System> compare two values> distance (Player.X, Player.Y, Touch.X,Touch.Y) > 300*dt

so my question is what do I have to do to be able to increase the speed of player movement based on touch position?

what events and actions do I have to add or change to allow for this to happen?

please bear in mind that I have only had construct 2 for 1 day so I apologise if this is very basic but I cant find any examples or tutorials on something like this.

• You might want to look into using Lerp , it will let you move to a distance based on time .

player>set x to lerp(self.x,touch.x,time*dt) ,where time can be input from a distance calculation.

• You might want to look into using Lerp , it will let you move to a distance based on time .

player>set x to lerp(self.x,touch.x,time*dt) ,where time can be input from a distance calculation.

Thanks for the input Granpa I added both lerp actions for X and Y and it just made the player move extremely fast towards the touch position hah, I need to make the distance calculation and use it instead of "time" but do I do it under a variable because it will change? or is it simply just another event?

I will google it to find the solution myself and If I do I will post back so I dont waste anyones time but meanwhile anyones input would be greatly appreciated.

thanks again

• ## Try Construct 3

Develop games in your browser. Powerful, performant & highly capable.

Construct 3 users don't see these ads
• Maybe you can create some rectangles and use them as "areas" and set: if touching rectangleX > set char speed to X; if touching rectangleY > set char speed to Y. Got it? If you put them on the HUD layer with 0 parallax they will be on the screen until you decide don't.

If you have any doubts, just let me know and I can make an example for you :3

• Maybe you can create some rectangles and use them as "areas" and set: if touching rectangleX > set char speed to X; if touching rectangleY > set char speed to Y. Got it? If you put them on the HUD layer with 0 parallax they will be on the screen until you decide don't.

If you have any doubts, just let me know and I can make an example for you :3

I dont think this would be appropriate because I want the speed to change based on touch distance from player, if I create rectangles and give each a certain speed that speed will be set even when the player is on said rectangle, so for example the player is currently on RectangleY and it wants to move towards rectagleX and it will do so at the speed set on rectagleX, lets assume RectagleX is the furthest point from RectangleY so the speed in this isntance would be the fastest possible; but if the player is in rectagleX and wants to move slightly to the left but still in RectangleX then it will still use the speed set on RectagleX whereas the speed should be considerably slower since the touch position is only slightly to the left of the player!

I doubt there is other ways of doing it other than distance calculations but do correct me if im wrong because I still havent figured out how to do it! lol

• I have managed to fix it now and the player moves correctly using distance calculations as its speed:

so the touch distance now affects the player speed by increasing its speed the further away the touch position is and likewise the player slows down as it reaches the touch position.

It was achieved by the following modifications to the events:

Original Events:

Touch> is in touch> Player > move 300*dt pixels at angle (self.X, self.Y, touch.X, Touch.Y)

System> compare two values> distance (Player.X, Player.Y, Touch.X,Touch.Y) > 300*dt

New Events:

Touch>is in touch> Player> move distance ( Player.X, Player.Y, Touch.X, Touch.Y)*dt pixels at angle (self.X,Self.Y,Touch.X,Touch.Y)

System> compare two values> distance (Player.X, Player.Y, Touch.X,Touch.Y) >= 300*dt