Legacy GM Sprite Direction Point to Mouse

S

Skyler Clark

Guest
So I’m making a top down game.

Right now the player is just a cube, (there’s the invisible cube for the actual player input, and another visible cube drawn on top of it for the animation so it can spin) and the front of the cube spins around and looks wherever the mouse is.

I’m going to be adding a sprite with an actual character soon, so instead of having the sprite spin in a full 360 looking at the mouse all of the time, I want the sprite to only have 4 directions it can look; up, down, left, and right. I can’t think of how to go about this. It would mean a lot if anyone could send me some code or suggestions. Thanks!
 

Relic

Member
Code:
//grab direction from player to mouse
var mdir = point_direction(x,y,mouse_x,mouse_y)

//Convert the direction into a segment number by breaking circle into eight 45 deg chunks. Segment 0 covers 0 to 45 deg and segment 7 would 315 to 360 deg, so these two segments would be for when character is looking right.

var seg=mdir div 45

//set sprite direction
switch seg{
case 0:
case 7:
image_angle=0;
break;
case 1:
case 2:
image_angle=90
break;
//cont with rest of segments
}
 
S

Skyler Clark

Guest
Thanks man, does anything go after the cases or is it just how you have it?




Code:
//grab direction from player to mouse
var mdir = point_direction(x,y,mouse_x,mouse_y)

//Convert the direction into a segment number by breaking circle into eight 45 deg chunks. Segment 0 covers 0 to 45 deg and segment 7 would 315 to 360 deg, so these two segments would be for when character is looking right.

var seg=mdir div 45

//set sprite direction
switch seg{
case 0:
case 7:
image_angle=0;
break;
case 1:
case 2:
image_angle=90
break;
//cont with rest of segments
}
 

Relic

Member
Other than adding In the rest of the case statements for the other directions, that code is enough to set the image angle of the player to be a choice of right, up, left or down based on the angle from player to mouse.
 
S

Skyler Clark

Guest
I just got it working, thank you so much!

So whenever I have different sprites for each direction, I would just replace image_angle with changing the sprite?

Other than adding In the rest of the case statements for the other directions, that code is enough to set the image angle of the player to be a choice of right, up, left or down based on the angle from player to mouse.
 
S

Skyler Clark

Guest
Alright I know I’m starting to sound like such a noob but I don’t have much experience with the visual part of gamemaker. Right now I’m using

object_set_sprite(o_player_skin, s_player_up);

And so on with the rest of the directions. It’s not working. I feel like I’m using the wrong code for the wrong thing. I would really appreciate your help, thanks
 
S

Skyler Clark

Guest
Alright I know I’m starting to sound like such a noob but I don’t have much experience with the visual part of gamemaker. Right now I’m using

object_set_sprite(o_player_skin, s_player_up);

And so on with the rest of the directions. It’s not working. I feel like I’m using the wrong code for the wrong thing. I would really appreciate your help, thanks
Nvm, I figured it out. I change it to
sprite_index = s_player_right

It works perfectly now. Thanks for your time!
 
Top