Further to this thread
http://www.ofpec.com/forum/index.php?topic=32983.0and its demo's code, I've taken much of it (thanks guys!) and used it to find the Target(s) the Player is pointing at or locked onto.
assignedTarget works fine for AI, but not for players.
The below code gets me very close, except because it uses a trigger box
centered on the player/vehicle, the trigger not only overlaps the target in front, it also overlaps any target that may be 180 deg. behind the player. The trigger needs to only be forward of the player.
*** Need a little trigonometry help to
center the trigger box out half way to the max range of detection, in line with the weapon facing (then rotate the trigger box).
BTW, the principle of this, place a long thin trigger box stretching out from the player in the direction of the weapon, wait 1 sec, then collect details of all targets within that long thin detector.
In my case, I will add code to select the closest of any targets found, and then send a Torpedo at it !
In general, because the trigger box is thin, it should mostly only find the 1 target.
Feed from "FIRED" EH
private["_ships", "_trigger", "_array", "_msg", "_dist", "_target", "_unit", "_weapon", "_range"];
_array = _this select 0;
_unit = _array select 0;
_weapon = _array select 1;
_range = 3000;
_trigger = createTrigger ["EmptyDetector", getPos _unit];
_trigger setTriggerActivation ["ANY", "PRESENT", false];
_trigger setTriggerArea [50, (_range/2), (((_unit weaponDirection _weapon) select 0) atan2 ((_unit weaponDirection _weapon) select 1)), true];
_trigger setTriggerType "NONE";
_trigger setTriggerStatements ["this", "", ""];
_trigger setTriggerTimeout [0, 0, 0, false];
_ships = [];
Sleep 1.0;
{
if (_x isKindOf "Ship") then
{
if ((isEngineOn _x) && (side _x == West)) then
{
_ships = _ships + [_x];
};
};
} forEach list _trigger;
if (count _ships > 0) then
{
_msg = "";
{
_target = _x;
_dist = _target distance _unit;
_msg = _msg + format["Target:%1 - Range: %2\n", _target, _dist];
} forEach _ships;
hint format ["%1",_msg];
};
deletevehicle _trigger;