Jump to content
Sign in to follow this  
Nielsen

Is there a simple way to find the nearest player of an ai unit?

Recommended Posts

Well the title says it all..

Or actually it does not have to be all that simple, if anyone would show me a way to do this.

The 'nearestObject' does not seem fit for this, or I am using it wrong.

Any thoughts?

Regards,

Share this post


Link to post
Share on other sites

No the ai is civilian and friendly to the player.

I'm not sure how I would use the nearestObjects to achieve this. Any help?

Share this post


Link to post
Share on other sites

What exactly are you trying to do? What's the situation that the civ and player are in? If you just want to see if a player is within 50m, nearestObject would work. If you want to test a larger area nearestObjects would work. You could also try nearTarget or nearEntities probably, but knowing exactly what you're wanting to do would make finding a solution a lot easier.

Share this post


Link to post
Share on other sites

I think using one of the nearest commands might not be most optimal. Since he is looking for a player unit. We already know those, they are in playableunits.

One minute result, but you get the idea:

_unit = playableunits select 0;
_closest = [_unit,_unit distance _this];
for "_i" from 1 to (count playableunits - 1) do {
 _unit = playableunits select _i;
 _dist = _unit distance _this;
 if (_dist < _closest select 1) then {
   _closest = [_unit,_dist];
 };
};

Share this post


Link to post
Share on other sites

@Shk:

Awesome as allways man. I'll finish my dinner and episode of South Park, and then I'll try to wrap my head around the code and implement it. Will report back once tested.

@Kylania and ST DUX:

Thanks for trying to help out. Basically I need to do exactly what I said. I spawn a civ, and then I want to know which player is closest to that civ. No more and no less.

:)

Share this post


Link to post
Share on other sites

@Shk:

Works like a charm!

Hehe, I spent hours on this, and I just could not get it to work. I was in the process of writing this post crying for help, when it dawned on me, that 'playableunits' might not work in the editor :rolleyes:.

It took me a while to fully comprehend the code, even though its not that long and quite simple. It's rather clewer, at least by my standards :). The time I used trying to get it to work was well spent learning.

for helping me out once again! :worship: Edited by Nielsen

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  

×