GMS 2 Move Player Towards Mouse/Touch x, y

Hello,

I'm making a mobile game where it's played in portrait mode and the screen is auto scrolling upward with the o_player moving in a top down style movement and while I have some input methods already done, I'm trying to make an input method where when the player "touches" a part of the screen the o_player moves along with the finger when dragging on the screen.

Like what is seen with some of the arcade style space shooting games.

I already have been experimenting with this line of code
Code:
o_player.x = mouse_x;
o_player.y = mouse_y;
But instead of moving along with the touch, the o_player "jumps" to that position.

I'd like to make it where the player can touch anywhere on the screen and while dragging their finger the o_player moves along with the movement of the finger without having to directly touch the o_player.

Also, I'd want the o_player to still recognize the solid collision I set up, since when using the o_player.x = mouse_x; o_player.y = mouse_y; method the o_player can be drug through solids.
 
Last edited:

TheouAegis

Member
if point_distance(x,y,mouse_x,mouse_y) > 2 move_towards_point(mouse_x,mouse_y,2)
else speed = 0;

It's a simple code, no acceleration included, though. The distance you check should be no less than half your max movement speed.
 
if point_distance(x,y,mouse_x,mouse_y) > 2 move_towards_point(mouse_x,mouse_y,2)
else speed = 0;

It's a simple code, no acceleration included, though. The distance you check should be no less than half your max movement speed.
Thank you for the code. But I aleady tried something like that and when the o_player should be stopping (like during a dialog) it keeps moving and goes off the screen.

I wish I could give a better example. But like the player touches the screen and drags left or right and no matter where the o_player is, the o_player doesn't move "towards the touch" but moves along with it. Also I noticed I did have a typo that said moves towards.
 

TheouAegis

Member
When the player should be stopping, you should be stopping the player.

So you want the player to move equal to the finger, but not TO the finger?

Code:
//Begin Step Event
mouse_xprevious = mouse_x;
mouse_yprevious = mouse_y;
Code:
//Step Event
hspeed = mouse_x - mouse_xprevious;
vspeed = mouse_y - mouse_yprevious;
if global.dialog_pause {
    hspeed = 0;
    vspeed = 0;
}
 
When the player should be stopping, you should be stopping the player.

So you want the player to move equal to the finger, but not TO the finger?

Code:
//Begin Step Event
mouse_xprevious = mouse_x;
mouse_yprevious = mouse_y;
Code:
//Step Event
hspeed = mouse_x - mouse_xprevious;
vspeed = mouse_y - mouse_yprevious;
if global.dialog_pause {
    hspeed = 0;
    vspeed = 0;
}
That's correct. I want the o_player to move WITH the finger not TO the finger. Basically, I'm wanting the o_player to move in whichever direction the finger is currently dragging without having to touch the o_player or to have the o_player move towards the touch.

After trying your code in several ways with my movement code the o_player does noting and sometimes moves only a slight amount.

The reason I'm wanting to use this method is because, like I said when using the o_player.x = mouse_x; o_player.y = mouse_y; method, the o_player jumps to wherever a touch was. Making it where the o_player can move over colisions and most likely avoid any enemies in the room.
 
T

Tiilerdye

Guest
I’m not understanding what you are trying to do, maybe a visual example is warranted. If sounds like you want the object to move towards the mouse and abide by collisions. Maybe try out something like mp_potential_step_object(mouse_x,mouse_y,3,objWall);
 
I’m not understanding what you are trying to do, maybe a visual example is warranted. If sounds like you want the object to move towards the mouse and abide by collisions. Maybe try out something like mp_potential_step_object(mouse_x,mouse_y,3,objWall);
Here's a small example of what I'm talking about. Also, some of the space shooters on Google Play have this type of control.

 

pipebkOT

Member
if you want to drag the player, but without touching the player , global dragging events is the solution for you. there are some tutorials in youtube.



the video shows scrolling, but is the same logic behind the dragging


the code is in the description, so you can copy and paste it in your project
 
Last edited:
if you want to drag the player, but without touching the player , global dragging events is the solution for you. there are some tutorials in youtube.



the video shows scrolling, but is the same logic behind the dragging
THANK YOU!!! That was exactly what I wanted!!
 
Top