L
Lobos
Guest
Hi! I'm trying to make a top-down shooter, but got stuck at the beginning (literally).
(the question is in the very end)
First, I wrote a super basic code for moving (in the step event):
and a collision check (still in the step event):
Thats working reasonably well, except if there is a wall: I could glued to it, so when I read this article:
https://www.yoyogames.com/blog/432/buttery-smooth-tech-tips-movement
I copied it (the script):
with the following step event:
My problem is: it is using place_free, which require solid walls, but as the 14th commandments of Game Maker said, you don't use solid. But without it (and with this code), the player go throu everything, including walls.
Could you help me to modify the script (or suggest an easy-to-use better one), so the player/enemies stop next to the wall without solid walls?
At first I simply tied to replace place_free with place_meeting [like: if !place_meeting(xtarg,ytarg,obj_wall) ], but thats not working, nor my other attemps (or I screwed up something).
(the question is in the very end)
First, I wrote a super basic code for moving (in the step event):
Code:
///Player movement
var player_horizontalspeed = 0;
var player_verticalspeed = 0;
//Player input
if (keyboard_check(ord('W')))
{
player_verticalspeed -= player_movement_forward;
}
if (keyboard_check(ord('A')))
{
player_horizontalspeed -= player_movement_left;
}
if (keyboard_check(ord('S')))
{
player_verticalspeed += player_movement_backward;
}
if (keyboard_check(ord('D')))
{
player_horizontalspeed += player_movement_right;
}
Code:
//Collision checking
if (place_meeting(x + player_horizontalspeed,y, obj_wall))
while (!place_meeting(x + sign(player_horizontalspeed), y, obj_wall))
x += sign(player_horizontalspeed);
else
x += player_horizontalspeed;
if (place_meeting(x, y + player_verticalspeed, obj_wall))
while (!place_meeting(x, y + sign(player_verticalspeed), obj_wall))
y += sign(player_verticalspeed);
else
y += player_verticalspeed;
//Look at the direction of the mouse
player_point_direction = point_direction(x,y,mouse_x,mouse_y);
image_angle += sin(degtorad(player_point_direction - image_angle)) * player_movement_rotationspeed;
Thats working reasonably well, except if there is a wall: I could glued to it, so when I read this article:
https://www.yoyogames.com/blog/432/buttery-smooth-tech-tips-movement
I copied it (the script):
Code:
var spd = argument0;
var dir = argument1;
var xtarg = x+lengthdir_x(spd,dir);
var ytarg = y+lengthdir_y(spd,dir);
if place_free(xtarg,ytarg) {
x = xtarg;
y = ytarg;
}
else {
var sweep_interval = 10;
for ( var angle = sweep_interval; angle <= 80; angle += sweep_interval) {
for ( var multiplier = -1; multiplier <= 1; multiplier += 2) {
var angle_to_check = dir+angle*multiplier;
xtarg = x+lengthdir_x(spd, angle_to_check);
ytarg = y+lengthdir_y(spd, angle_to_check);
if place_free(xtarg,ytarg) {
x = xtarg;
y = ytarg;
exit;
}
}
}
}
Code:
var move_xinput = 0;
var move_yinput = 0;
for ( var i = 0; i < array_length_1d(movement_inputs); i++){
var this_key = movement_inputs[i];
if keyboard_check(this_key) {
var this_angle = i*90;
move_xinput += lengthdir_x(1, this_angle);
move_yinput += lengthdir_y(1, this_angle);
}
}
var moving = ( point_distance(0,0,move_xinput, move_yinput) > 0 );
if moving {
var move_dir = point_direction(0,0,move_xinput, move_yinput);
move(move_speed_this_frame, move_dir);
}
My problem is: it is using place_free, which require solid walls, but as the 14th commandments of Game Maker said, you don't use solid. But without it (and with this code), the player go throu everything, including walls.
Could you help me to modify the script (or suggest an easy-to-use better one), so the player/enemies stop next to the wall without solid walls?
At first I simply tied to replace place_free with place_meeting [like: if !place_meeting(xtarg,ytarg,obj_wall) ], but thats not working, nor my other attemps (or I screwed up something).