0
\$\begingroup\$

When the enemy move's to the right their gun faces right. When they move left the gun will face left as it should. When the player collides with the enemy collision line on the right side, the enemy gun will point towards the player properly.

When the player is on the left side of the enemy and hits the collision line, the enemy gun will sort of point towards the player but not facing the right direction.

 Step Event of Enemy gun object:

//image_xscale = 1 is looking to the right, -1 is left
// I use image_angle so the enemy gun will aim at the player on x and y axis

if obj_testEnemy.image_xscale == 1 { //When the enemy is looking to the right
    if (collision_line(x, y, x + 128, obj_testPlayer.y , obj_testPlayer, false, true)) {
        image_angle = point_direction(x,y,obj_testPlayer.x,obj_testPlayer.y); //how it aims at the player
    } else {
        image_angle = obj_testEnemy.image_xscale; // faces the correct direction when not colliding
    }

} else {
    if (collision_line(x, y, x - 128, obj_testPlayer.y , obj_testPlayer, false, true)) {
        image_angle = point_direction(x,y,obj_testPlayer.x,obj_testPlayer.y);
        image_xscale = -1; //gun should be facing left but it's not
    }
}

I don't understand why the gun won't face left when the collision line is facing left

Working:

This is the side working

Not Working:

Not working

\$\endgroup\$
3
  • \$\begingroup\$ What does "sort of point towards the player but not facing the right direction" mean? Can you embed some screenshots so we can see the problem you're seeing? \$\endgroup\$
    – DMGregory
    May 3, 2020 at 1:06
  • \$\begingroup\$ I added some pictures of it working when the enemy is facing right and not working when the enemy is facing left. \$\endgroup\$ May 3, 2020 at 1:42
  • \$\begingroup\$ Perhaps you should give the gun sprite more details to tell the difference which is the front, right now it's difficult to tell if it's facing correct or backwards. You may also check the origin point if that changes as well. \$\endgroup\$
    – Steven
    May 4, 2020 at 6:41

1 Answer 1

0
\$\begingroup\$

Well, I've done a lot of thinking and decided to try a collision circle instead. This is what I've come up with and it has also solved my problem of the gun not aiming properly! here are my changes:

if !collision_circle(x,y,128,obj_testPlayer,true,true) {

image_xscale = sign(xspd);

if image_xscale == 1 {
    obj_testGun.image_angle = 0;
} else {
    obj_testGun.image_angle = 180;
}

} else {

obj_testGun.image_angle = point_direction(x,y,obj_testPlayer.x, obj_testPlayer.y);

if obj_testPlayer.x > x { 
    image_xscale = 1;
} else {
    image_xscale = -1;
}
}

Here's a link to see it in action: https://www.youtube.com/watch?v=wRhcnLi82E4&feature=youtu.be

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .