Jump to content
Sign in to follow this  
Wilsdorf

getpos nearest player?

Recommended Posts

Hello everybody,

I have one civilian and I want to know the nearest player.

And then the civilian move to his position.

Thanks

Share this post


Link to post
Share on other sites

Adapt or place this in whatever file fits your needs.

//--- Sorting function
SortByDistance = {
Private["_current","_nearest","_nearestDistance","_object","_objects","_sorted"];

_object = _this select 0;
_objects = +(_this select 1);

_sorted = [];
for '_i' from 0 to count(_objects)-1 do {
	_nearest = ObjNull;
	_nearestDistance = 100000;

	for '_j' from count(_objects)-1 to 0 step -1 do {
		_current = _objects select _j;
		_distance = _current distance _object;
		if (_distance < _nearestDistance) then {_nearest = _current;_nearestDistance = _distance};
	};

	_sorted = _sorted + [_nearest];
	_objects = _objects - [_nearest];
};

_sorted
};

//--- Determine the players if MP, else use player itself.
_units = [player];
if (isMultiplayer) then {
_units = [];
{if (isPlayer _x) then {_units = _units + [_x]}} forEach playableUnits;
};

//--- civxyz can be replaced by the center (your civilian for instance).
_sorted = [civxyz, _units] Call SortByDistance;

//--- The array return units by order, in our case, we want the closest one.
_closest = _sorted select 0;

//--- Move the civilian to the position of the closest player.
civxyz move (position _closest);

Share this post


Link to post
Share on other sites

Shorter method of getting nearest player:

_nearest=objNull;
_nearestdist=10000;
{
_dist=vehicle _x distance civ1;
if (isPlayer _x and _dist<_nearestdist) then {
	_nearest=_x;
	_nearestdist=_dist;
};
} forEach playableUnits;

if (!isNull _nearest) then {civ1 move getPos vehicle _nearest};

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  

×