• 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!

Direction problems?

B

Brian Le

Guest
Hi guys, I am making a game about police and thieves.
I want to code something like this: when the player is inside the police's view, he (the police) will rotate the gun and shoot at the thief. The bullet direction must be same as the gun angle, and the gun angle follows the player. However, when the bullet is shot and flies far away the police, suddenly the player jumps, the bullet changes its direction. Here is my code and object list:
Obj_player Create Event:
Code:
///Initialize variables
image_speed = 0.3;
grav = 0.2;
hsp = 0;
vsp = 0;
jumpspeed = 6;
movespeed = 4;
rotang = 3;

can_shoot = true;
health = 100;
instance_create(0,0,obj_assualtrifle);
Obj_player Alarm 0:
Code:
can_shoot = true;
Obj_player Step Event:
Code:
///Player controller
//Initialize variables
var dir = point_direction(x,y,mouse_x,mouse_y);
//var dir1,dir2,key_right,key_left,key_up,key_down;

//Player's input
key_right = keyboard_check(ord('D'));
key_left = -keyboard_check(ord('A'));
key_up = keyboard_check_pressed(ord('W'));
key_down = keyboard_check(ord('S'));

//React to the inputs
var move = key_right + key_left;
hsp = move * movespeed;
if (vsp < 10) {
    vsp += grav;
}
if (place_meeting(x,y+1,par_wall)) {
    if (key_up != 0) vsp = -jumpspeed;
}

//Horizontal Collision checking
if (place_meeting(x+hsp,y,par_wall)) {
    while (!place_meeting(x+sign(hsp),y,par_wall)) {
        x += sign(hsp);
    }
    hsp = 0;
}
x += hsp;

//Vertical Collision checking
if (place_meeting(x,y+vsp,par_wall)) {
    while (!place_meeting(x,y+sign(vsp),par_wall)) {
        y += sign(vsp);
    }
    vsp = 0;
}
y += vsp;

//Animate
if (move != 0) {
    image_xscale = move;
    with (obj_assualtrifle) {
        image_xscale = obj_player.image_xscale;
    }
}
if (place_meeting(x,y+1,par_wall)) {
    if (move != 0) {
        sprite_index = spr_player_run;
    }
    else {
        if (key_down != 0) {
            sprite_index = spr_player_duckling;
        }
        else {
            sprite_index = spr_player;
        }
    }       
}
else {
    if (vsp < 0) {
        sprite_index = spr_player_jump;
    }
    else {
        sprite_index = spr_player_fall;
    }
}

//Weapon setup
//1. Assualt Rifle
with (obj_assualtrifle) {
    x = obj_player.x+1;
    y = obj_player.y+2;
}
key_gunup = keyboard_check(ord('T'));
key_gundown = -keyboard_check(ord('G'));
var rot = key_gunup + key_gundown;
switch (obj_assualtrifle.image_xscale) {
    case 1: {
        obj_assualtrifle.image_angle += rot * rotang;
        break;
    }
    case -1: {
        obj_assualtrifle.image_angle += -rot * rotang;
        break;
    }
}
if (obj_assualtrifle.image_angle > 65) {
    obj_assualtrifle.image_angle = 65;
}   
if (obj_assualtrifle.image_angle < -65) {
    obj_assualtrifle.image_angle = -65;
}
key_shoot = keyboard_check(vk_space);
if (key_shoot != 0 && can_shoot) {
    can_shoot = false;
    alarm[0] = room_speed/4;
    switch (obj_assualtrifle.image_xscale) {
        case 1: {
            inst = instance_create(obj_assualtrifle.x+lengthdir_x(16,obj_assualtrifle.image_angle),
            obj_assualtrifle.y+lengthdir_y(16,obj_assualtrifle.image_angle),obj_assualtbullet);
            inst.direction = obj_assualtrifle.image_angle;
            inst.image_angle = inst.direction; 
            break;
        }
        case -1: {
           inst = instance_create(obj_assualtrifle.x-lengthdir_x(16,obj_assualtrifle.image_angle),
           obj_assualtrifle.y-lengthdir_y(16,obj_assualtrifle.image_angle),obj_assualtbullet);
           inst.direction = obj_assualtrifle.image_angle - 180;
           inst.image_angle = inst.direction - 180;
           break;   
        }
    }
    inst.speed = 10;
}
The player's gun works well (when the player's gun changed the direction, the bullet still flies in its original direction).

Par_police Create_Event:
Code:
inst = instance_create(0,0,obj_police_gun);
knockback = 6;
hsp = -3;
image_speed = 0.3;
police_health = 100;
can_shoot = true;
Par_police Alarm 0 Event:
Code:
sprite_index = spr_police;
inst.visible = true;
Par_police Alarm 1 Event:
Code:
can_shoot = true;
Par_police Step Event:
Code:
//Initialize variables
//Jumping gun
inst.x = x-2;
inst.y = y-2;

//Image_xscale control
switch (image_xscale) {
    case 1: {
        inst.image_xscale = 1;
        break;
    }
    case -1: {
        inst.image_xscale = -1;
        break;
    }
}
// Moving
if (place_meeting(x+sign(hsp)*5,y,par_wall)) {
    hsp *= -1;
    image_xscale *= -1;
    knockback = 0;
}
x += hsp;
if (hsp = 0) {
    knockback = 6;
    if (place_meeting(x+(sign(hsp)+1)*5,y,par_wall)) {
        knockback = 0;
    }
}

//Animate
if (hsp != 0) {
    sprite_index = spr_police_run;
}

//Cone Vision
inst2 = instance_nearest(x,y,obj_player);
if (instance_exists(inst2)) {
    var x1,y1,x2,y2;
    x1 = x - sign(image_xscale)*lengthdir_x(250,image_angle - 45);
    y1 = y - sign(image_xscale)*lengthdir_y(250,image_angle - 45);
    x2 = x - sign(image_xscale)*lengthdir_x(250,image_angle + 45);
    y2 = y - sign(image_xscale)*lengthdir_y(250,image_angle + 45);
    if (rectangle_in_triangle(inst2.bbox_left,inst2.bbox_top,inst2.bbox_right,inst2.bbox_bottom,x,y,x1,y1,x2,y2)) {
        switch (inst.image_xscale) {
            case 1: {
                inst.image_angle = point_direction(x,y,obj_player.x,obj_player.y) - 180;
                if (can_shoot) {
                    inst3 = instance_create(inst.x+lengthdir_x(18,inst.image_angle-180),inst.y+lengthdir_y(18,inst.image_angle-180),obj_police_bullet);
                }
                can_shoot = false;
                alarm[1] = room_speed/4;
                inst3.speed = 7;
                inst3.direction = inst.image_angle-180;
                inst3.image_angle = inst3.direction;
                break;
            }
            case -1: {
                inst.image_angle = point_direction(x,y,obj_player.x,obj_player.y);
                if (can_shoot) {
                    inst3 = instance_create(inst.x+lengthdir_x(18,inst.image_angle),inst.y+lengthdir_y(18,inst.image_angle),obj_police_bullet);
                }
                can_shoot = false;
                alarm[1] = room_speed/4;
                inst3.speed = 7;
                inst3.direction = inst.image_angle;
                inst3.image_angle = inst3.direction;
                break;
            }
        }
    }
    else {
        inst.image_angle = 0;   
    }
}
Par_police Collision with Bullets event:
Code:
sprite_index = spr_police_damaged;
inst.visible = false;
hsp = 0;
x += sign(image_xscale) * knockback;
if (obj_player.x > x) {
    image_xscale = -1;
}
else {
    image_xscale = 1;
}
alarm[0] = room_speed/7;
police_health -= 3;
Par_police Draw Event:
Code:
//I draw this just for a better look at cone vision
draw_self();
var x1,y1,x2,y2;
x1 = x - sign(image_xscale)*lengthdir_x(250,image_angle - 45);
y1 = y - sign(image_xscale)*lengthdir_y(250,image_angle - 45);
x2 = x - sign(image_xscale)*lengthdir_x(250,image_angle + 45);
y2 = y - sign(image_xscale)*lengthdir_y(250,image_angle + 45);
draw_triangle_colour(x,y,x1,y1,x2,y2,c_lime,c_aqua,c_red,true);
Also, I have some pictures for better illustrations:
upload_2017-7-10_10-45-12.png
After that the bullet changes the direction:
upload_2017-7-10_10-45-58.png
 

Attachments

samspade

Member
I see two possible problems. First, your step event directly references the bullet with the following code:

Code:
            case -1: {
                inst.image_angle = point_direction(x,y,obj_player.x,obj_player.y);
                if (can_shoot) {
                    inst3 = instance_create(inst.x+lengthdir_x(18,inst.image_angle),inst.y+lengthdir_y(18,inst.image_angle),obj_police_bullet);
                }
                can_shoot = false;
                alarm[1] = room_speed/4;
                inst3.speed = 7;
                inst3.direction = inst.image_angle;
                inst3.image_angle = inst3.direction;
                break;
            }
My guess is that what is happening is that this case is often true and because it is true your police officer's step event is affecting the bullet every single step. Multiple solutions, but what I normally do is something like this:

Code:
///step event
if (can_shoot) && (alarm[0] == -1) alarm[0] = shot_timer;

///alarm[0]
var bullet =  instance_create(inst.x+lengthdir_x(18,inst.image_angle),inst.y+lengthdir_y(18,inst.image_angle),obj_police_bullet);
with (bullet) {
    direction = other.image_angle;
    image_angle = direction;
}

///bullet create event
speed = 7;
direction = 0;
image_angle = 0;
You'll probably need more code that that, but what it does if the gun can shoot and the alarm assigned to shooting == -1 (which means the alarm is not set) it sets the alarm to whatever you have predefined as the shot_timer. When that alarm goes off it creates the bullet and then accesses the bullet one time, and one time only, and sets its direction and image angle. Important note that the bullet object's create event runs before the with (bullet) statement in the alarm to the best of my knowledge so the create event variables will be overwritten.


Alternatively, the problem could be that you have code in the bullet step event which tracks the player.
 
Top