Dr_Nomz
Member
I have a few moments where an object is supposed to collide with something based on it's positions from where it is now to where it will be in the next step, and then run the correct code. The first moment the bullet is created it can't collide with anything at first, so the first moment it's created it acts a little differently, relying on previousx/y and current x/y, that's why there are 4 collisions here. (2 each for Walls/Enemies)
Thing is I can't seem to make it work, because sometimes bullets will hit through walls and if you're too close to a wall, the bullet will be destroyed before it hits the enemy. How do I make it so the bullet collides with what it's supposed to collide with? (Whichever object is closer, it'll react to that one instead of the other.)
Thing is I can't seem to make it work, because sometimes bullets will hit through walls and if you're too close to a wall, the bullet will be destroyed before it hits the enemy. How do I make it so the bullet collides with what it's supposed to collide with? (Whichever object is closer, it'll react to that one instead of the other.)
Code:
//Variables for predicting collision in the bullet's next position.
var x2 = x + lengthdir_x(speed, direction);
var y2 = y + lengthdir_y(speed, direction);
//Wall Collision.
if collision_line(xprevious,yprevious,x,y,obj_Wall,false,false) && wall_collision = 0{
//For colliding at the start of the bullet's creation.
instance_destroy();
}
if collision_line(x,y,x2,y2,obj_Wall,false,false) && wall_collision < 1{
//For predicting it's next point and colliding before it draws it's position there.
instance_destroy();
}
//Enemy collision.
if collision_line(xprevious,yprevious,x,y,par_Enemy,false,false) && wall_collision = 0{
with(collision_line(xprevious,yprevious,x,y,par_Enemy,false,false)){
scr_Enemy_Damage(argument0);
}
instance_destroy();
}
if collision_line(x,y,x2,y2,par_Enemy,false,false) && wall_collision < 1{
with(collision_line(x,y,x2,y2,par_Enemy,false,false)){
scr_Enemy_Damage(argument0);
}
instance_destroy();
}