3D [Solved] Moving left and right :^) ty for everybody

C

CornerLord

Guest
Im tying to make my character move left and right but im so stupid that i dont know how to do that.
Here is my create event code:
Code:
/// Initialize the player
z = 16;
dir  = 0;

// Create target variables for movement
target_dir = dir;
target_x = x;
target_y = y;

// Set the default draw color to white
draw_set_colour(c_white);

//Start 3D drawing
d3d_start();
And here is my step event code where the movement is. I can make my character only move backwards and forwards.
Code:
if keyboard_check(ord('W')) {
// Move forward
    var vect_x = lengthdir_x(1.5, target_dir);
    var vect_y = lengthdir_y(1.5, target_dir);

// Move if not wall
    if (!place_meeting(target_x+vect_x, target_y+vect_y, obj_wall))
    {
        target_x += vect_x;
        target_y += vect_y;
    }
}
if keyboard_check(ord('S')) {
// Move backward
    var vect_x = lengthdir_x(1, target_dir);
    var vect_y = lengthdir_y(1, target_dir);

// Move if not wall
    if (!place_meeting(target_x-vect_x, target_y-vect_y, obj_wall))
    {
        target_x -= vect_x;
        target_y -= vect_y;
    }
}
 

YellowAfterlife

ᴏɴʟɪɴᴇ ᴍᴜʟᴛɪᴘʟᴀʏᴇʀ
Forum Staff
Moderator
Left/right would have `target_dir - 90` instead of `target_dir`, that's all of trouble.
 

Joe Ellis

Member
For turning:
you need to make target_dir decrease when you press left and increase when you press right

For moving:
set vect_x to lengthdir_x(1.5, target_dir-90) (-90 for left, +90 for right)
and vect_y to lengthdir_y(1.5, target_dir-90)

do the collision check and movement the same as before:

// Move if not wall
if (!place_meeting(target_x+vect_x, target_y+vect_y, obj_wall))
{
target_x += vect_x;
target_y += vect_y;
}

example code:

Code:
if keyboard_check(ord('A')) {
// Move left
    var vect_x = lengthdir_x(1.5, target_dir-90);
    var vect_y = lengthdir_y(1.5, target_dir-90);

// Move if not wall
    if (!place_meeting(target_x+vect_x, target_y+vect_y, obj_wall))
    {
        target_x += vect_x;
        target_y += vect_y;
    }
}
if keyboard_check(ord('D')) {
// Move right
    var vect_x = lengthdir_x(1, target_dir-90);
    var vect_y = lengthdir_y(1, target_dir-90);

// Move if not wall
    if (!place_meeting(target_x-vect_x, target_y-vect_y, obj_wall))
    {
        target_x -= vect_x;
        target_y -= vect_y;
    }
}
or:

Code:
if keyboard_check(ord('A')) {
// Move left
    var vect_x = lengthdir_x(1.5, target_dir-90);
    var vect_y = lengthdir_y(1.5, target_dir-90);

// Move if not wall
    if (!place_meeting(target_x+vect_x, target_y+vect_y, obj_wall))
    {
        target_x += vect_x;
        target_y += vect_y;
    }
}
if keyboard_check(ord('D')) {
// Move right
    var vect_x = lengthdir_x(1, target_dir+90);
    var vect_y = lengthdir_y(1, target_dir+90);

// Move if not wall
    if (!place_meeting(target_x+vect_x, target_y+vect_y, obj_wall))
    {
        target_x += vect_x;
        target_y += vect_y;
    }
}
 
Top