• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GameMaker [SOLVED] Object Following Mouse Collision Issue

D

DrSlouch

Guest
Summary: I want an object to follow the mouse's position (by actually moving, not just being locked to the mouse's position) and if the object runs into a wall to stop, but not get stuck/become unable to ever move again.
EDIT3
Check the second post I made for a video, should make it even clearer.

Details:

So, I have a game where an object (a kite, with a 24x24 pixel sprite) follows the mouse around. However, when I set the object to simply do that, it stopped when the edges reached the mouse, which I didn't like. So I created an invisible 1-pixel object (oKiteCenter) that follows the mouse, and the kite (oKite) has its origin set to the center, and is set to keep its X and Y coordinates to oKiteCenter's. Now the kite follows the mouse, and always stops at the center, which is great, exactly what I wanted. The issue I'm having is once collision comes into the mix. Here's the code in the oKite step event:
Code:
if !place_meeting((x + oKiteCenter.speed), (y + oKiteCenter.speed), oWall)
{
   oKiteCenter.canmove = 1
}
if place_meeting((x + oKiteCenter.speed), (y + oKiteCenter.speed), oWall)
{
   oKiteCenter.canmove = 0
}
And the code in the oKiteCenter step event:
Code:
if canmove = 1
{
   if (distance_to_point(oKiteGoal.x,oKiteGoal.y) >= 4)
   {
       move_towards_point(oKiteGoal.x,oKiteGoal.y,4)
   }
   if (distance_to_point(oKiteGoal.x,oKiteGoal.y) < 4)
   {
       speed = 0
   }
}
if canmove = 0
{
   speed = 0
}

(oKiteCenter establishes canmove in its create event, and it's set to 1, for the record.)
As things are currently set up, oKite moves towards the wall and then gets stuck and can never move again. How would I change this? I've watched Shaun Spaulding's tutorials on movement with the keyboard for platformer characters, and I know he has a thing set up where once you're less than your speed away from a wall, there's a while statement causing you to move perfectly up to the wall, but I'm not sure how I can apply that in this scenario, since it's just "speed" in any direction and not strictly horizontal or vertical alone.
Thanks in advance and of course if more information is necessary I can provide it (I could even try to make a video if it's somehow not clear).

EDIT: Just realized, the code also mentions "oKiteGoal," that's another 1-pixel object that is set to be at the mouse's X and Y, and the reason I did that is so once the center touches the goal it stops, easily preventing that thing that happens where it moves 4 pixels towards something 3 pixels away, which makes it move past it, and then it goes back, etc., causing a loop of jittering.
EDIT2: Also also, the Wall objects against which I'm checking for collision are simple 16x16 perfectly square boxes, no angles to worry about.
 
Last edited by a moderator:
T

TheOakNuggins

Guest
If I understand correctly, you have two issues:

Problem 1: The kite's edges don't collide with walls, your invisible object does.
SOLUTION: set the collision mask to be the same as the kite.

Problem 2: You want the kite to follow the mouse, not necessarily snap to it.
SOLUTION: Maybe try and lerp it?
 
D

DrSlouch

Guest
RE: Solution 1 - If I set the invisible object to have the same collision as the Kite, won't it be unnecessary and I may as well just have the kite follow the mouse? Then the issue comes up where as soon as an edge of the kite touches the mouse, it stops moving, as opposed to having the center specifically come to the mouse's location.
RE: Solution 2 - I don't really have a problem with the kite following the mouse, the second code block accomplishes that fine as long as the kite doesn't collide with any walls. I think I'll record a video of how this all works to make this clearer.

EDIT
Here's a video of what I have going and the issue arising as I collide with a wall.
EDIT2 I learned about a bunch of new functions and swapped around some of the tasks I was setting each object to do, and now it works basically perfectly. Problem solved. If somehow someone is interested in what the code was I can post it but other than that I'm done.
 
Last edited by a moderator:
Top