Make a sprite pointing (rotating on his pivot center) towards the mouse cursor

E

Eyjmera

Guest
Hi,

i am basically trying to mimic the standard move of a simple 2D game named surviv.io
I succeeded to make the sprite move left, right, up and down but i can't make it rotate on himself to point on direction of the mouse cursor (like in my example). Any idea how i can do that ?

Thanks a lot !!
 

Attachments

Nocturne

Friendly Tyrant
Forum Staff
Admin
GML:
var _ang = point_direction(x, y, mouse_x, mouse_y);
var _diff = angle_difference(_ang, image_angle); // I may have the arguments here the wrong way round, so if it's not quite right, swap them!
image_angle += median(-5, _diff, 5); // change the 5, -5 values to make the rotation faster or slower
That's pretty much how you'll do it. Get the direction to the mouse, get the current image angle (or direction), then get the difference, then use that along with some min-max values to set the rotation. :)
 
E

Eyjmera

Guest
GML:
var _ang = point_direction(x, y, mouse_x, mouse_y);
var _diff = angle_difference(_ang, image_angle); // I may have the arguments here the wrong way round, so if it's not quite right, swap them!
image_angle += median(-5, _diff, 5); // change the 5, -5 values to make the rotation faster or slower
That's pretty much how you'll do it. Get the direction to the mouse, get the current image angle (or direction), then get the difference, then use that along with some min-max values to set the rotation. :)
Wow thanks a lot for the quick answer, i may have post it in the wrong channel, i use D&D cuz i don't know anything about coding. Does this mean that with standard node you can't do it ? This GML code, i can copy paste it somewhere ? In some node ? I am so sorry i am really a noob, all i know is photoshop and 3D software, i open GameMaker yesterday for the first time ^^

anyway thanks for your answer !!
 
Last edited by a moderator:

Nocturne

Friendly Tyrant
Forum Staff
Admin
Hello! Okay, so DnD would be like this:

1596436435807.png

Any time you see a function that isn't available in DnD you can still use the Function Call action to use it and then store any returned value in a variable. :)
 
E

Eyjmera

Guest
Hello! Okay, so DnD would be like this:

View attachment 33134

Any time you see a function that isn't available in DnD you can still use the Function Call action to use it and then store any returned value in a variable. :)

Hi, i've rebuild your nodal tree, i hope i got it right. I did that in the event "Mouse Enter" it kind of weirdly rotate when i move my mouse cursor but not like i wanted. Was it a mistake to choose Mouse Enter ? Am i suppose to do this in another kind of event ? Sorry i am so noob, but thanks you so much Nocturne for taking on your time to help me 🙏

Quentin
 
E

Eyjmera

Guest
Also when i type in the Function "point_direction" Gamemaker propose me "point_direction(x1,y1,x2,y2)" should i choose that or just stick to what you wrote ?
 

woods

Member
has been a couple days.. if you havnt figured it out yet.. this might help ;o)

i would put this in the step event of the player object.. err rather the object you are rotating towards the mouse..

as far as point_direction(x1,y1,x2,y2)

x1,y1 is the x/y of the object.. usually top left by default or center. you can adjust this manually in the sprite editor btw (the rotation point relative to the sprite)
x2,y2 is the point to reference to rotating to... in your case, the x/y of the mouse

====
boss tip.. if you dont understand a function hit F1 and bring up the manual.. you can search you function and gain a fair bit on how it works ;o)
 
Top