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

Collision Line Bullet Code

T2008

Member
I would like my bullet to adjusted it's direction if solid object is in collision line. Not sure of how to do this. My Bullet Code is below (relevant parts). It doesn't work; I added the collision line part, but originally just said direction = 0 (which didn't work also). The robot is in correct direction but because it's bullet is way smaller, it can hit a solid along a corner and player can hide behind wall while robot is just shooting corner of wall.

Any ideas would be greatly appreciated!

Bullet Create Event:
Code:
//Set Direction  
if (obj_robot_rng.sprite_index = spr_robot_rng_shoot_e) {
    if (!collision_line(x,y,obj_player.x,obj_player.y,obj_bullet_only_solid,false,false)) {
        direction = 0;
    } else { direction = 10; }
}
if (obj_robot_rng.sprite_index = spr_robot_rng_shoot_ne) {
    //direction = 45;
    if (!collision_line(x,y,obj_player.x,obj_player.y,obj_bullet_only_solid,false,false)) {
        direction = 45;
    } else { direction = 55; }
}
if (obj_robot_rng.sprite_index = spr_robot_rng_shoot_n) {
    //direction = 90;    
    if (!collision_line(x,y,obj_player.x,obj_player.y,obj_bullet_only_solid,false,false)) {
        direction = 90;
    } else { direction = 100; }
}
if (obj_robot_rng.sprite_index = spr_robot_rng_shoot_nw) {
    //direction = 135;
    if (!collision_line(x,y,obj_player.x,obj_player.y,obj_bullet_only_solid,false,false)) {
        direction = 135;
    } else { direction = 145; }
}
if (obj_robot_rng.sprite_index = spr_robot_rng_shoot_w) {
    //direction = 180;    
    if (!collision_line(x,y,obj_player.x,obj_player.y,obj_bullet_only_solid,false,false)) {
        direction = 180;
    } else { direction = 190;} 
}
if (obj_robot_rng.sprite_index = spr_robot_rng_shoot_sw) {
    //direction = 225;
    if (!collision_line(x,y,obj_player.x,obj_player.y,obj_bullet_only_solid,false,false)) {
        direction = 225;
    } else { direction = 235; }
}
if (obj_robot_rng.sprite_index = spr_robot_rng_shoot_s) {
    //direction = 270;
    if (!collision_line(x,y,obj_player.x,obj_player.y,obj_bullet_only_solid,false,false)) {
        direction = 270;
    } else { direction = 280; }
}
if (obj_robot_rng.sprite_index = spr_robot_rng_shoot_se) {
    //direction = 315;    
    if (!collision_line(x,y,obj_player.x,obj_player.y,obj_bullet_only_solid,false,false)) {
        direction = 315;
    } else { direction = 325; }
}
 

Rob

Member
Hey man if you check the manual for that function, it returns an instance_id or no one.
You're using it as though it returns a Boolean (true/false)

If you modify your code to this:

GML:
var colliding_with = collision_line(x,y,obj_player.x,obj_player.y,obj_bullet_only_solid,false,false);

if (colliding_width != noone){

if (obj_robot_rng.sprite_index = spr_robot_rng_shoot_e) direction = 10;
if (obj_robot_rng.sprite_index = spr_robot_rng_shoot_ne) direction = 55;
if (obj_robot_rng.sprite_index = spr_robot_rng_shoot_n) direction = 100;
if (obj_robot_rng.sprite_index = spr_robot_rng_shoot_nw) direction = 145;
if (obj_robot_rng.sprite_index = spr_robot_rng_shoot_w) direction = 190;
if (obj_robot_rng.sprite_index = spr_robot_rng_shoot_sw) direction = 235;
if (obj_robot_rng.sprite_index = spr_robot_rng_shoot_s) direction = 280;
if (obj_robot_rng.sprite_index = spr_robot_rng_shoot_se) direction = 325;
  
}else{
//not colliding


if (obj_robot_rng.sprite_index = spr_robot_rng_shoot_e) direction = 0;
if (obj_robot_rng.sprite_index = spr_robot_rng_shoot_ne) direction = 45;
if (obj_robot_rng.sprite_index = spr_robot_rng_shoot_n) direction = 90;
if (obj_robot_rng.sprite_index = spr_robot_rng_shoot_nw) direction = 135;
if (obj_robot_rng.sprite_index = spr_robot_rng_shoot_w) direction = 180;
if (obj_robot_rng.sprite_index = spr_robot_rng_shoot_sw) direction = 225;
if (obj_robot_rng.sprite_index = spr_robot_rng_shoot_s) direction = 270;
if (obj_robot_rng.sprite_index = spr_robot_rng_shoot_se) direction = 315;


}
Also - I see you're using Object. to reference instances. If you only ever have one instance of each object, that's ok, otherwise you should be using instance_id's such as collision_line returns. You can middle-mouse-click on a function to see what it does and what it returns.
 
Last edited:

T2008

Member
Thanks for the response! I didn't realize that about that function. I tried your suggestion and the bullet still hits the solid. I was thinking there was some way to do a while loop for it to check, ie the loop would keep checking to see if there was collision line free between bullet and player and once it is, the bullet would use that direction. I don't know how I would write this code though.
 
Top