Raised This Month: $51 Target: $400
 12% 

How can I make NPCs avoid obstacles ?


Post New Thread Reply   
 
Thread Tools Display Modes
Black Rose
Veteran Member
Join Date: Feb 2011
Location: Stockholm, Sweden
Old 04-20-2021 , 13:33   Re: How can I make NPCs avoid obstacles ?
Reply With Quote #11

Quote:
Originally Posted by thezolotoi View Post
Hello, how can I get my npc to use this include for moving? Tried to understand the example, but could not.
Checking the forum, I found a function so that the entity could jump over small obstacles, but there was still a question about avoiding obstacles.
There are design choices you can make along the way that will make the code more or less efficient.

The most important one is when is the pathing "out of date" and need to be updated?

Another one might be, how is it going to know when to switch point to the next one. You can do it by touching an entity which you keep moving to the next point. Another way is by simply calculating if the distance to the next point is close enough.

But for a simple example:

Create your path between two points, for example an entity chasing a player:
Code:
new Float:fStart[3], Float:fEnd[3]; pev(myent, pev_origin, fStart); pev(id, pev_origin, fEnd); gMyPathIndex = AStarThreaded(fStart, fEnd, "PathDone");

And when the path is done:
Code:
new Array:gPath; public PathDone(Index, Array:hPath, Float:Distance, NodesAdded, NodesValidated, NodesCleared) {     if ( hPath == Invalid_Array )     {         client_print(0, print_chat, "Pathfinding failed.");         return;     }     gPath = hPath; }
If you have several pathfindings you might want to check if gMyPathIndex equals Index.

If there is a path, make your ent follow each step of it.
Code:
public myentThink(ent) {     if ( gPath != Invalid_Array )     {         if ( ArraySize(gPath) )         {             new Float:vOrigin[3];             entity_get_vector(ent, EV_VEC_origin, vOrigin);             new iNextPoint[3], Float:fNextPoint[3];             ArrayGetArray(gPath, 0, iNextPoint); // Get the next point in the path.             IVecFVec(iNextPoint, fNextPoint); // Convert it to a float for distance calculation.             if ( get_distance_f(fNextPoint, vOrigin) >= 5 ) // Check if the entity is close enough to the point it's moving towards for it to get a new point to move towards.                 ArrayDeleteItem(gPath, 0); // The new point won't be the target until next think but it shouldn't really matter. Otherwise you have to implement the same code twice for several steps.             // Do movement towards iNextPoint/fNextPoint.         }         else             ArrayDestroy(gPath); // The end was reached, destroy the array.     }     // Update nextthink... }

When it comes to ent parameters or how to actually move it in the correct way I'm of no help.
__________________

Last edited by Black Rose; 04-20-2021 at 13:41.
Black Rose is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 06:02.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode