GameMaker: Studio

GameMaker: Studio

View Stats:
moving player object with mouse
Thought I solved this but apparently not. I want to control the movement of my player object with the mouse. I tried this in the step event of the player object:

move_towards_point (mouse_x, mouse_y, 5), but this causes the player object to trail the mouse cursor. This is not exactly what I want as you need to be aware of where the mouse cursor is at all times to allow accurate movement. I wish to hide the mouse cursor. I also don't want the player object to be the mouse cursor because then movement speed is based on the speed of the mouse. Is there another solution?
< >
Showing 1-10 of 10 comments
Blyxx Sep 27, 2013 @ 4:01am 
In create event:

prevMouseX = mouse_x;
xMove = 0;

In step event:

if mouse_x > prevMouseX
{
xMove += 1;
}

else if mouse_x < prevMouseX
{
xMove -= 1;
}

x += xMove;

In end step event:

prevMouseX = mouse_x;

It'll be really hard to stop moving with this control scheme, I think that might be intentional for your controls? If not, you might want a manual way for the player to stop moving

In keystroke/mousePressed/whatever trigger you desire event:

xMove = 0;
prevMouseX = mouse_x;

Hope that helps.
Sera Sep 27, 2013 @ 9:38am 
Alternatively, you could just move the difference entirely, give or take a speed cap.

I know mouse camera controls typically reset the mouse position every frame, which would let you omit the prevMouse variables if you can do similar (I forget is mouse_x is read only, but I think there's a function somewhere, worst case) and also would stop the edge of the screen being hit, a situation which would result in the player suddenly being unable to move because the mouse can't move any further in a given direction, rendering nobody very happy.
regardtvisagie Sep 28, 2013 @ 1:42am 
Thanks for the response. Blyxx, your code works like I want it to. I don't want the acceleration effect though (for now at least) so instead of "xMove += 1" I only used "xMove = 1". Perhaps you can help me further. I also want an y axis, in other words, vertical movement. I used the same code but only replaced the x's with y's but for some reason the movement is no longer predictable or smooth if you like. I know it's because the mouse movement is very sensitive but how can I mitigate this effect so that movement is responsive but still accurate?
regardtvisagie Sep 28, 2013 @ 1:48am 
basically, I want WASD movement replaced by mouse movement
Blyxx Sep 28, 2013 @ 6:47am 
You want 8 directions to move in or 4? Doing 8 directions will require adding some new variables and conditionals, and will probably always feel unpredictable unless you have very precise hand-eye coordination.

For 4 directions, you'll need to alter the step event to compare the difference between mouse_x and prevMouseX and the difference between mouse_y and prevMouseY, then move horizontally or vertically based on which is greater:

if abs(mouse_x - prevMouseX) > abs(mouse_y -prevMouseY)
{
if mouse_x > prevMouseX
{
xMove = 1;
}

else if mouse_x < prevMouseX
{
xMove = -1;
}

x += xMove;
}

else if abs(mouse_x - prevMouseX) < abs(mouse_y -prevMouseY)
{
[same stuff, but for y]
}

As long as your player makes an effort not to move the mouse at 45 degree angles, one and only one of the conditions will evaluate, giving you 4 distinct directions to move in.
regardtvisagie Sep 28, 2013 @ 7:44am 
Sorry, I realise now that I confused you by comparing the movement I want with WASD movement as this implies either 4 or 8 directions. I actually want free movement (360 if you will), if at all possible, but if not I suppose 8 directions will have to suffice. 4 directions is a bit too limited for my purposes - thanks anyway.
Blyxx Sep 28, 2013 @ 8:01am 
"Free movement" can be done with the mouse, but there are many different ways to do it, it would be a lot easier to visualize what you want if you describe your game a bit as opposed to describing your mechanics.
regardtvisagie Sep 28, 2013 @ 10:32am 
Good idea. Let me try. It's a type of melee fighting game. You control the weapon (sword etc.) with the mouse to aim where to strike. The body of the enemy is a simple 2D body viewed from the front. So you can aim for the shoulder, head, torso etc. Firstly, I just want to get the basic controls right. I tried controlling the sword with WASD and it worked, but its too labourous. I thought the mouse would work better as it will feel like your swinging the sword but it's difficult to implement. Later I was hoping to implement stats like weapon speed, weapon control to the movement so that I player with higher stats will have more speed, control etc. (For example, I player with a heavy weapon, like a hammer, but low on weapon control, would have difficult controlling the weapon as oppose to a light spear). Hope this gives you a better idea. By the way, I greatly appreciate your help.
Blyxx Sep 29, 2013 @ 3:44pm 
That sounds like a fun concept, but hard to implement. From what you've said in this thread and your previous one, there are several things you want to avoid with your controls:

-no visible mouse cursor
-don't trail the mouse (because there's no visible cursor)
-no clicking of the mouse necessary
-object shouldn't simply "become" the mouse cursor

I'm having a hard time picturing a control scheme that meets all of those criteria. What about this: Use left/right movement of the mouse to rotate the sword like a clock hand to point toward your intended target. When you have the attack lined up the way you want, swipe the mouse upward to launch it. To return your sword to ready position, swipe the mouse downward. Would that work? It would be similar to the four direction control scheme, but instead of moving the object to the left and right in the x-movement checks, it would rotate the image of your weapon clockwise or counter clockwise. In the y-movement checks, swiping upward would move the sword in the direction its been pointed in at whatever speed you desire. The downward swipe would move it back to ready position (will need variables to assign this at creation) and reset the angle of its rotation.

I feel like this probably isn't exactly what you're looking for, but I can't picture anything else right now that meets the requirements. Fun problem to think about though!
regardtvisagie Sep 30, 2013 @ 3:32am 
Although this isn't what I was looking for initially, it might actually work perfectly. Thanks a lot. I had a feeling that free movement (360) was a bit too free to implement in a structured way. Your suggestion can solve this problem as it still give you plenty of options without the weapons flying all over the screen. Thanks again. Now, I have no idea how to implement this...
< >
Showing 1-10 of 10 comments
Per page: 1530 50

Date Posted: Sep 27, 2013 @ 3:42am
Posts: 10