Jump to content
Sign in to follow this  
wok

Make AI follow player

Recommended Posts

I am doing some scripts and part of it involves an AI enemy being captured and then asked to follow the player. My simplified code looks something like this:

	
   target = cursorTarget;
   caller = _this select 1;
   target setBehaviour "CARELESS";

   fnc_substr =
   {
           _str1 = toArray(_this select 0);

           _start = _this select 1;
           _length = (_this select 2) - 1;
           _substr = [];

           for "_i" from 0 to _length do
           {
                   _substr set [_i, _str1 select (_start + _i)];
           };

           toString _substr
   };

   while{ alive target && alive caller } do {
           target doMove position caller;
           _distance = caller distance target;

           if( _distance > 5 ) then {
                   target setSpeedMode "FULL";
           } else {
                   if( [animationState caller, 9, 3] call fnc_substr == "wlk" ) then {
                           target setSpeedMode "LIMITED";
                   };

                   if( [animationState caller, 9, 3] call fnc_substr == "run" ) then {
                           target setSpeedMode "NORMAL";
                   };

                   if( [animationState caller, 9, 3] call fnc_substr == "eva" ) then {
                           target setSpeedMode "FULL";
                   };
           };

           sleep 2;
   };

I call it with a simple addAction while aiming at an enemy unit. The important part is target doMove position caller;, the rest is just so the enemy will copy the speed of the player (walk, run, etc). The code works but the result is not very pretty, the enemy does follow the player but it looks like one of the zombies from dayz. They always have to move to your last known position instead of taking the shorter path, and they do weird stuff like stopping and continuing specially while walking.

Does someone have a better code for doing this or have some ideas for improving mine?

Share this post


Link to post
Share on other sites

joinCasual (AI basically following player, imitating his stance, but no visual in units tab, control, etc.) command would be useful.

Share this post


Link to post
Share on other sites
The only other way i can think of is to make them join your group.

Yea, I wanted to avoid that.

Share this post


Link to post
Share on other sites

Instead of moving him to the position of the player, try moving him to a position 5 or 10 meters in front of player. Stop him if player stops, so that he never truly gets ahead. This may give him a longer caluclated distance to travel, which tends to make for less retarded-ness.

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  

×