Jump to content
Sign in to follow this  
colonel_klink

Detect any enemy, unit, vehicle, of any side

Recommended Posts

This problem has been vexing me for a couple of days now. In the sandbagged GPMG positions I've been working on I'm wanting to run a script that will detect any enemy, unit, vehicle, of any side that approaches the position. If say the enemy was detected by the machine gunner at say 20 meters from any direction (particularly from behind) the gunner would 'get out' and use his rifle etc or run off.

I can use the distance command to detect how far a unit is away from the gunner, but how, without using triggers can I detect any unit at all just using a script.

Any help in this would be appreciated.

Share this post


Link to post
Share on other sites

well you should use getnearestobject.

Set the checkpoint around 20-50 meter behind the mg nest and check lets say every second. Then you should filter the returned object (if an object is returned).

Maybe you could also use 3-5 checkpoints, so you can check a wider field behind the gunner position.

Share this post


Link to post
Share on other sites

I thought of that, but as far as I know nearestobject (couldn't find getnearestobject) relies on a known object, e.g. nearestObject [player, "StreetLamp"]. What I'm after is away of detecting the unit (vehicle, etc) and then determine if it is friend of foe.

Share this post


Link to post
Share on other sites

right...there is no getnearestobject, I just wrote it out of my mind.

nearestobject [positionarray] will return the nearest object -no matter what type- found around there.

After getting the object you can filter it (mainly over "side" and "distance").

Share this post


Link to post
Share on other sites

I've tried  

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">_Badguy =nearestobject[_gunpos]  

(_gunpos being the position of _gun retreived by getpos) and also tried:

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">nearestobject[_gunpos] =_badguy

as well as:

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">_Badguy =nearestobject[_x,_y,_z]

but each time I got an error. I'm probably not doing this correctly. Thanks Rastovovich

Share this post


Link to post
Share on other sites

SYNTAX: nearestObject[POSITION,TYPE]

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">_badguy = nearestObject[_gunpos,""]

or

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">_badguy = nearestObject[_gunpos,]

one of these should be the correct one

don't you have the ComRef? You find such things in there  wink_o.gif

Share this post


Link to post
Share on other sites

_badguy = nearestobject [_pos]

(No "," in the array)

should work.

Should return you the mggunner itself.

Share this post


Link to post
Share on other sites

Ok here's what i got so far

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">

_gun = _this select 0

_player = player

_gunner = gunner _gun

; _deadmeat=""

_x1 = getpos _gun select 0

_z1 = getpos _gun select 1

_y1 = getpos _gun select 2

_PosGun = getpos _gun

? _player in _gun : goto "end"

? _gunner in _gun : goto "start"

#start

~0.001

_deadmeat = nearestobject [_x1,_z1,_y1]

;?_deadmeat="": goto "Start"

hint format["Enemy = %1",_deadmeat]

_dist = _deadmeat distance _gun

? (_dist <= 6): goto "Detected"

? _gunner in _gun : goto "start"

goto "start"

#End

exit

#detected

_gunner action ["getout", _gun]

All I get with this using the hint command to return the detection:

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">

hint format["Enemy = %1",_deadmeat]

is

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">

Enemy - 14ee4100# NOID

bunkergpmg

The bunkergpmg is the name of the p3d object and not the name of the in game GPMG unit.

It's got me beat.

@hardrock. Yep got comref but I'm still a learning this stuff smile_o.gif

Share this post


Link to post
Share on other sites

Of course it returns your gun, since you look for the nearest object around your pos, which is your gun.

You need to search some meters away (~30 meter behind the gun)

Share this post


Link to post
Share on other sites

But shouldn't it return the unit name and not the model name? Also I need to determine whether the unit is an enemy. Thanks

Share this post


Link to post
Share on other sites

Well, I tried it with the following code and it worked.

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">; Syntax in initline of gun is: (this) exec "script.sqs"

? format ["%1",side _this] == "EAST" : _enemy = ["WEST", "RESISTANCE"]

? format ["%1",side _this] == "WEST" : _enemy = ["EAST"]

_gunner = gunner _this

_i = ((getdir _this) - 90) Mod 360

_i2 = 0

_posarray = []

#calculate

_posarray = _posarray + [[((getpos _this select 0) - cos(_i)*30), ((getpos _this select 1) - sin(_i)*30),0]]

? _i2 < 5 : _i = _i - 36, _i2 = _i2 + 1, goto "calculate"

_i = 0

#checkposition

? (format ["%1",side nearestobject (_posarray select _i)] in _enemy) : unassignvehicle _gunner, exit

? _i < 5 : _i = _i + 1, goto "checkposition"

~0.5

? !alive _this : exit

_i = 0

goto "checkposition"

BTW, you don't need the units name. You only need to know if it is enemy.

Edit: The 2 lines directly under #calculate are infact on line in the code, the 2 lines under checkposition aswell.

Share this post


Link to post
Share on other sites

Ok will try that. Thanks Rastovovich.

edit!

Working great now. Thanks again.

Now for the next bit... how to link a new dying animation to the existing gunner action.... Animation is done... however:)

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

×