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:
Obj_player Alarm 0:
Obj_player Step Event:
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:
Par_police Alarm 0 Event:
Par_police Alarm 1 Event:
Par_police Step Event:
Par_police Collision with Bullets event:
Par_police Draw Event:
Also, I have some pictures for better illustrations:
After that the bullet changes the direction:
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);
Code:
can_shoot = true;
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;
}
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;
Code:
sprite_index = spr_police;
inst.visible = true;
Code:
can_shoot = true;
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;
}
}
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;
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);
After that the bullet changes the direction:
Attachments
-
11.8 KB Views: 1