MobyCoding
MobyCoding

Reputation: 3

How to make an object move in relation to image_angle?

Note: I'm using GameMaker 1.4, not 2. I don't know if this makes a difference, but I'm pointing it out just in case.

Okay, so I'm having an issue in my GameMaker game where I'm making an object move up and down in relation to image_angle. For some reason, it really wants to move across the y axis as normal and completetly disregards the image_angle. This is really annoying and could change the game entirely if not fixed.

My code is for the step event is:

// Mouse controls.
image_angle = point_direction(x, y, mouse_x, mouse_y);

if(keyboard_check(ord('W')))
{
    y -= playerSpeed;
}

if(keyboard_check(ord('S')))
{
    y += playerSpeed;
}

My code for the create event is:

globalvar fuelRemaining;
fuelRemaining = 60;

playerSpeed = 3;

Upvotes: 0

Views: 4580

Answers (3)

user7003504
user7003504

Reputation:

In your code, you set image_angle and direction, but then change only y coordinate so of course you wont see any changes to x coordinate.

You can use built-in direction and speed variables and game-maker will move any object automatically, or you can apply lengthdir_x and lengthdir_y functions on x and y coordinates.

direction and speed example:

//create event

playerSpeed = 3;

//step event

image_angle = point_direction(x, y, mouse_x, mouse_y);
direction = image_angle;
speed = 0; //reset speed at start of each step event
           //now you wont have to check if key has been released to stop object

if(keyboard_check(ord('W')))
{
    //distance between player and target
    distance = point_distance(x, y, mouse_x, mouse_y)

    if distance < playerSpeed
    {
        //with this, object will move only to mouse position, not further
        speed = distance;
    }
    else
    {
        speed = playerSpeed;
    }
}
else if(keyboard_check(ord('S')))
{
    speed = -playerSpeed;
}

lengthdir_x and lengthdir_y example:

// create event

playerSpeed = 3;

// step event

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

if keyboard_check( ord("W") )
{
    x += lengthdir_x( playerSpeed, image_angle );
    y += lengthdir_y( playerSpeed, image_angle );
}
else if keyboard_check( ord("S") )
{
    x += lengthdir_x( playerSpeed, image_angle-180 );
    y += lengthdir_y( playerSpeed, image_angle-180 );
}

Please note that all game-maker functions use angle in degrees, with angle 0(360) at right side of circle, increasing counter-clockwise. Any functions work properly with values both under and above 0 to 360 range, while for example -90 = 270 (360-90) and 400 = 40 (400-360). Beware of how game-maker checks true and false. Any value < than 0 will result as false on check.

Upvotes: 0

Dmi7ry
Dmi7ry

Reputation: 1777

@Gamio code is almost correct (should be _released instead of _release, confused the sign for W and S keys). Here are few improvements:

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

if point_distance(x, y, mouse_x, mouse_y) < spd
{
   speed = 0;
}
else
{
    var spd = 0;

    if keyboard_check(ord("W")) 
        spd += playerSpeed; // If pressed both W and S, then will stop 

    if keyboard_check(ord("S"))
        spd -= playerSpeed;

    speed = spd;

    if keyboard_check_released(ord("W")) or keyboard_check_released(ord("S"))
        speed = 0;
}

Place the code in Step End event (not Step).

If you want change coords directly, without speed, it is also possible, but little more difficult (you need calculate both x and y deltas using lengthdir_x and lengthdir_y functions and then add these deltas to current coords).

Upvotes: 0

Gamio
Gamio

Reputation: 1

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

if(keyboard_check(ord('W'))){
    speed = -playerSpeed;
}
if(keyboard_check_release(ord('W'))){
    speed = 0;
}

if(keyboard_check(ord('S'))){
    speed = playerSpeed;
}
if(keyboard_check_release(ord('S'))){
    speed = 0;
}

Upvotes: 0

Related Questions