0
votes

I'm trying to guide the player to the last enemy on the field, via having an icon on the edge of the screen pointing towards the last enemy outside the screen.

The screen is the rectangle, the player is the point within the rectangle, and the enemy is the point outside of the rectangle.

Example

The solutions I can think of are finding which side will be intersected (Which I'm not sure how to do correctly, but I imagine first involves finding if it will be intersecting on vertical or horizontal sides) and then using a linear equation to find the x or y. Or, you could use a line-line-intersection method on each side, but as the screen rect never changes, this seems a little overkill.

I've got my first solution working on paper for one test case, but can't get it working at all with in Unity.

Does anyone have a solution or could push me in the right direction? Thanks a lot.

1

1 Answers

0
votes

Let's rectangle edges have equation

X = XLeft
Y = YBottom
X = XRight
Y = YTop

Player-enemy vector

D = (D.X, D.Y) = (E.X - P.X, E.Y - P.Y)

if D.X is positive, consider intersection with right edge (otherwise with left)

P.X + t * D.X = XRight

if D.Y is positive, consider intersection with top edge (otherwise with bottom)

P.Y + u * DY.Y = YTop

Solve equation for t and u. If u is less than t, intersection with horizontal edge goes first, and find X-coordinate of intersection with expression

XInt =  P.X + u * D.X