• 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 Rotating Cursor Around Player?

W

Wiffo

Guest
Hey guys, I'm trying to create something like the attached image, with the little cursor always being a set distance away from the player character and rotating to where your mouse is pointed.

I don't really know where to start. I was thinking about just creating the cursor object at a set distance from the character and rotate towards wherever the mouse is, but I'm not very sure how to do that.

All I have right now is:

cursor_sprite = sCursor;
x = mouse_x;
y = mouse_y;

image_angle = point_direction(x,y,mouse_x,mouse_y);​

But that code is obviously not working. Can someone help? How do I keep the object at a set distance away from my character, and rotate the cursor?

Thank you.

 

Rob

Member
You're looking for a vector - basically a point that's calculated a set distance away from the player at an angle (and the angle changes to create the circular motion effect).

Check out this for an explanation on lengthdir_x/y

The lengthdir_x/y coordinates will give you the x/y that your cursor needs to be at any given moment
 
Hi! This is a quick example of how you could use lenghtdir_x and lengthdir_y functions:

Code:
oCursor's STEP EVENT =====================================================
if (instance_exists(oPlayer)) {

   var dis = 32;  // Distance from the player to the cursor
   var dir = point_direction(x, y, mouse_x, mouse_y);   // Cursor pointing at mouse coords

  // Clamp the direction (if you want the cursor to rotate only within a specific range).
   dir = clamp(dir, 0, 180);

  // Calculate x and y coords for the cursor
   x = oPlayer.x + lengthdir_x(dis, dir);
   y = oPlayer.y + lengthdir_y(dis, dir);

   // Rotate the cursor sprite
   image_angle = dir;
}
Hope that could help you!
 

Alexx

Member
An easier approach would be offset the sprite origin and have it fixed to the middle of the player.

If you need an example, just ask.
 
Top