# How to check if a player is facing another...

Discussion in 'Programming' started by BigCat, Mar 3, 2019.

Tags:
1. ### BigCatMember

Joined:
Oct 30, 2016
Posts:
2
I'm building a top-down shooter.

I have a target-A which basically sits in the center of the room
I have ships which fly around the room in various formation patterns or whatever.
The ships have laserbeams that fire directly forward.

I want the ships, as they fly about the room - to check whether they are facing target-A - and if so - shoot their lasers.

I have everything figured out - EXCEPT - how to check if an instance is facing another instance.
I've tried: if direction = point_angle ( x, y, target.x, target.y )
I've tried: if image_angle = point_angle ( x, y, target.x, target.y )

But I'm missing something or using it wrong - I dunno. It runs fine and doesn't throw any errors that I can see. But it just doesn't pull the trigger when it's supposed to.

My best guess - was that the two values are really never the same because of the rotation of the enemy ships. They rotate at a specific rate per frame which likely makes the two sides of the equation basically impossible without a relative range in place.

SO - My next attempt was something like
IF direction - point_angle (x,y, target.x, target.y) <= 20
But nope... that didn't do diddly either.

Any ideas or insight would be great.
I'm using DND - but I'm not afraid to write complex expressions.

Last edited: Mar 3, 2019
2. ### NightFrostMember

Joined:
Jun 24, 2016
Posts:
2,008
Directions and angles are floats, so it might be that the facing's off by 0.0001 degrees, but that still means they ain't equal and no lasers get fired. Instead you need to check that the difference is small enough, then shoot the laser. In that, angle_difference() is your friend.

BigCat likes this.
3. ### BigCatMember

Joined:
Oct 30, 2016
Posts:
2
BOOM!

@NightFrost saves my bacon!

By getting the angle_difference between image_angle, and point_angle ; I was able to create a safe fire zone it could find.

THANK YOU !!!!