• 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] Smooth rotating to point at mouse

M

MatthewL019

Guest
Hello. I'm currently developing a top-down game that involves rotating the player sprite to face the mouse's position.

I am aware of the common trick:
Code:
image_angle = point_direction(x,y,mouse_x,mouse_y);
However this looks very choppy to me, and I would like to implement a smoothing feature.

I tried using lerp for the first time, and here are my results (gifs):
https://gyazo.com/8767afff21180549aef3a67dcb92d5e3
https://gyazo.com/e38eb1309835a7893f702216058868be

Here is the code:
Code:
// Direction
dir = point_direction(x,y,mouse_x,mouse_y);
image_angle = lerp(image_angle, dir, 0.1);
It seems to work perfectly, actually, apart from one small thing... When dir goes from 0 to 360 (at the right side) it totally messes up the lerp and spins the character around...

Anyone have a solution? Thanks!
 

Gamerev147

Member
Set up 2 variables in the create event of the object you want to rotate:
Code:
///Local Variables
rspeed = 18; //The higher the values, the faster the rotation
dir = 0; //Direction used only for rotation
Then in the step event of that same object put the following code:
Code:
dir = point_direction(x, y, mouse_x, mouse_y);
image_angle += sin(degtorad(dir - image_angle)) * rspeed;
Some basic math can help with anything! Good luck with your game! :);)
 
Top