create a new script, call it something nice like "rotate_toward"
Roughly:
Code:
image_angle += min(sign(angle_difference(image_angle, target_angle) * rotation_speed), angle_difference(image_angle, target_angle));
that's a very good piece of code.
you can add it into the script you can create, like this:
Code:
/// rotate_toward( angle, speed )
target_angle = argument[0];
rotation_speed = argument[1];
image_angle += min(sign(angle_difference(image_angle, target_angle) * rotation_speed), angle_difference(image_angle, target_angle));
Now, in your code, replace "image_angle" by this new "rotate_toward" script you just created:
Code:
if( keyboard_check( ord('W') ) // Up
{
motion_set( 90,4 );
rotate_toward( 90, 8 ); // change the 8 for rotation speed
}
NOTE
The rotation will stop, even if not facing the right direction, if you release the key before it finishes.
If you want the rotation to complete, you could do something like this:
Code:
if( keyboard_check( ord('W') ) )
{
// code to go up
}
if( keyboard_check( ord('S') ) )
{
// code to go down
}
if( keyboard_check( ord('A') ) )
{
// code to go left
}
if( keyboard_check( ord('D') ) )
{
// code to go up
}
// If not pressing any of the 4 directions
if( !keyboard_check(ord('W')) and !keyboard_check(ord('A')) and !keyboard_check(ord('S')) and !keyboard_check(ord('D')) )
{
if( image_angle != target_angle ) // if rotation is not completed
{
rotate_toward( target_angle, rotation_speed ); // continue the current rotation
}
}
-- EDIT --
In the above piece of code, to avoid GameMaker complaining about variables being used before being declared, you should add both 'target_angle=0 and rotation_speed=0' to the create event of your object.
Hopefully this helps you solve your current issue ~
Regards,
CedSharp