1. Hey! Guest! The 34th GMC Jam will take place between August 22nd, 12:00 UTC (Thursday noon) and August 26th, 12:00 UTC (Monday noon). Why not join in! Click here to find out more!
    Dismiss Notice

GMS 2 Move Player Towards Mouse/Touch x, y

Discussion in 'Programming' started by KyleRansford, Aug 12, 2019.

  1. KyleRansford

    KyleRansford Member

    Joined:
    Oct 29, 2018
    Posts:
    71
    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: Aug 12, 2019
  2. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    6,661
    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.
     
  3. KyleRansford

    KyleRansford Member

    Joined:
    Oct 29, 2018
    Posts:
    71
    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.
     
  4. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    6,661
    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;
    }
     
  5. KyleRansford

    KyleRansford Member

    Joined:
    Oct 29, 2018
    Posts:
    71
    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.
     
  6. Tiilerdye

    Tiilerdye Member

    Joined:
    Jan 2, 2017
    Posts:
    21
    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);
     
  7. KyleRansford

    KyleRansford Member

    Joined:
    Oct 29, 2018
    Posts:
    71
    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.

    [​IMG]
     
  8. pipebkOT

    pipebkOT Member

    Joined:
    Dec 13, 2018
    Posts:
    208
    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: Aug 13, 2019
  9. KyleRansford

    KyleRansford Member

    Joined:
    Oct 29, 2018
    Posts:
    71
    THANK YOU!!! That was exactly what I wanted!!
     
  10. KyleRansford

    KyleRansford Member

    Joined:
    Oct 29, 2018
    Posts:
    71
    The only other thing I would need to do is have it to reconize colisions. Since for some reason when the o_player is controlled by a mouse or touch it goes right through colisions.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice