Can I make it so that units can only detect enemies from the front (ie in their line of sight)? How?
Can I make it so that a hit from the back is an instant kill? How?
Thanks in advance.
I can't completely answer your questions, but here are some ideas.
1. You could make the enemies neutral passive and have a trigger that fires every .1 seconds or something that checks to see if the player is in their forward vision, then change them to hostile.
To detect if the player is in their forward vision might actually be tough. The problem is that unit facing is giving in 0 to 360 terms. 0 is the right side of your screen, 180 is the left, for example. But if you compute the angle "between two points" it gives you the angle in -180 to 180 terms, with 0 being the right of your screen and 180 and -180 being the left. this means you can't just check the absolute value difference between facing and the angle of the two units to determine whether the enemy is looking in the direction of the player.
I'm hoping there's a better work around, but here's what I did:
In thinking terms: imagine a clock with 0 at 3o'clock, -90 at 6, 180/-180 at 9, and 90 at 12o'clock. we want to know the difference between the hour and minute dials. But since the numbers are screwy we can't just look at the difference between the numbesr. For example, if one was at 9:01 (179 in game speak) and the other at 8:59 (-179 in game speak) the difference would be 358, not 2 which is the right answer. So what we do is hold the hands of the clock constant and move the hour hand to "0". Then the difference between the two hands is the actual difference.
set GoodGuyPoint = position of GoodGuy
set EnemyPoint = position of Enemy
set math = to facing of Enemy
if math is >180, then set math to math -360
// this makes math the facing of the enemy in -180 to 180 terms
set math = 0 - math
// this makes "math" the difference between where the enemy is looking and 0, which will be useful later.
set math2 = angle between EnemyPoint and GoodGuyPoint
//math2 is where the hero is relative to the enemy
set math2 = math2 + math
//this moves the "second hand" so that it preserves its relative position to the other hand.
if math2 > 180 set math2 = math2 - 360
if math2 < -180 set math2 = math2 + 360
//those two statements correct math2 if it went above or below the normal limits when you added math1.
Custom Script: ***Kill GoodGuy and Enemy Point. If forgot the exact text, but this keeps it from leaking... you can look it up in the leak tutorial, you may already know it, and if someone reads this after me and knows, please post it**** The trigger will work without this, but it'll lag eventually.
Now you can use math2 to determine whether the unit is in front of him. Something like:
if abs(math2) < 90 then YES else NO
The "90" there means the player will have to be in a cone of 90 degrees. make it 180 if you want to give the guards good peripheral vision.
Using something similiar can determine if the player is behind the guard. change the last to if "abs(math) > 180" and it will detect if the player is behind him. It won't detect whether the target is in front of the player though (if they're back to back it will still fire) but assuming your attacks require you to face the target that won't be a problem.