Gamestudio Links
Zorro Links
Newest Posts
Help with plotting multiple ZigZag
by degenerate_762. 04/30/24 23:23
M1 Oversampling
by 11honza11. 04/30/24 08:16
Trading Journey
by howardR. 04/28/24 09:55
Zorro Trader GPT
by TipmyPip. 04/27/24 13:50
Data from CSV not parsed correctly
by jcl. 04/26/24 11:18
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
0 registered members (), 950 guests, and 3 spiders.
Key: Admin, Global Mod, Mod
Newest Members
firatv, wandaluciaia, Mega_Rod, EternallyCurious, howardR
19050 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
NPC follow path A to B back to A #407528
09/14/12 20:13
09/14/12 20:13
Joined: May 2006
Posts: 30
awstar Offline OP
Newbie
awstar  Offline OP
Newbie

Joined: May 2006
Posts: 30
Hi I tried coding a path for an NPC but it only goes to A, then B, then when it gets to A it stops. I was trying to get it to patrol a path from A to B then back, and so on.

Im not sure where to put the code properly on this forum so I will post it on this message for now:
Code:
ENTITY* goal;

 ENTITY* goal2;
function goal2_tracker();
function goal_tracker();
action npc_goal() // attach this action to the destination entity

{
c_setminmax(my);
	pXent_settype(my,PH_RIGID,PH_BOX);// movable 
       goal = my;        

}
action npc_goal2() // attach this action to the destination entity

{
c_setminmax(my);
	pXent_settype(my,PH_RIGID,PH_BOX);// movable 
       goal2 = my;        

}
 

action npc_tracker() // attach this action to your NPC character

{

       while (!goal) {wait (1);} // wait until the goal entity is loaded

       VECTOR temp, temp_angle;

       var npc_speed = 5;

       var covered_dist, i;

       while (1)

       {

               my.skill10 += 6 * time_step; // 6 gives the animation speed

               vec_set (temp.x, my.x); // trace 10,000 quants below the npc entity

               temp.z -= 10000;

               temp.z = -c_trace (my.x, temp.x, IGNORE_ME | IGNORE_PASSABLE | USE_BOX) + 20; // play with 20

               temp.x = npc_speed * time_step;

               temp.y = 0;

               ent_animate(my, "walk", my.skill10, ANM_CYCLE); 

               covered_dist = c_move (my, temp.x, nullvector, IGNORE_PASSABLE | GLIDE);

               if (covered_dist < 0.1) // the npc is stuck?

               {

                       my.pan += 90 - random(340); // then add a random angle to its pan angle

                       i = 0;

                       while (i < 80) // walk in the new direction for 10 frames, play with 10

                       {

                               c_move (my, temp.x, nullvector, IGNORE_PASSABLE | GLIDE);

                               ent_animate(my, "walk", my.skill10, ANM_CYCLE); 

                               i++;

                               wait (1);

                       }                                

               }

               else // the npc can move? Then rotate it towards the goal again!

               {

                       vec_set(temp_angle, goal.x);

                       vec_sub(temp_angle, my.x);

                       vec_to_angle(my.pan, temp_angle);

                       my.tilt = 0;

               }

               if (vec_dist(goal.x, my.x) < 80) // the npc has found the goal entity?

                       break; // then get out of the while loop!
                  
               wait (1);
               

       }
 goal2_tracker(); 
       // the goal was reached here

//       ent_animate(my, "stand", 0, 0); // switch to "stand"

}

function goal_tracker() // attach this action to your NPC character

{

       while (!goal) {wait (1);} // wait until the goal entity is loaded

       VECTOR temp, temp_angle;

       var npc_speed = 5;

       var covered_dist, i;

       while (1)

       {

               my.skill10 += 6 * time_step; // 6 gives the animation speed

               vec_set (temp.x, my.x); // trace 10,000 quants below the npc entity

               temp.z -= 10000;

               temp.z = -c_trace (my.x, temp.x, IGNORE_ME | IGNORE_PASSABLE | USE_BOX) + 20; // play with 20

               temp.x = npc_speed * time_step;

               temp.y = 0;

               ent_animate(my, "walk", my.skill10, ANM_CYCLE); 

               covered_dist = c_move (my, temp.x, nullvector, IGNORE_PASSABLE | GLIDE);

               if (covered_dist < 0.1) // the npc is stuck?

               {

                       my.pan += 90 - random(340); // then add a random angle to its pan angle

                       i = 0;

                       while (i < 80) // walk in the new direction for 10 frames, play with 10

                       {

                               c_move (my, temp.x, nullvector, IGNORE_PASSABLE | GLIDE);

                               ent_animate(my, "walk", my.skill10, ANM_CYCLE); 

                               i++;

                               wait (1);

                       }                                

               }

               else // the npc can move? Then rotate it towards the goal again!

               {

                       vec_set(temp_angle, goal.x);

                       vec_sub(temp_angle, my.x);

                       vec_to_angle(my.pan, temp_angle);

                       my.tilt = 0;

               }

               if (vec_dist(goal.x, my.x) < 80) // the npc has found the goal entity?

                       break; // then get out of the while loop!
                  
               wait (1);
                 

       }
       
function goal2_tracker();
       // the goal was reached here

//       ent_animate(my, "stand", 0, 0); // switch to "stand"

}
function goal2_tracker() // attach this action to your NPC character

{

       while (!goal2) {wait (1);} // wait until the goal entity is loaded

       VECTOR temp, temp_angle;

       var npc_speed = 5;

       var covered_dist, i;

       while (1)

       {

               my.skill10 += 6 * time_step; // 6 gives the animation speed

               vec_set (temp.x, my.x); // trace 10,000 quants below the npc entity

               temp.z -= 10000;

               temp.z = -c_trace (my.x, temp.x, IGNORE_ME | IGNORE_PASSABLE | USE_BOX) + 20; // play with 20

               temp.x = npc_speed * time_step;

               temp.y = 0;

               ent_animate(my, "walk", my.skill10, ANM_CYCLE); 

               covered_dist = c_move (my, temp.x, nullvector, IGNORE_PASSABLE | GLIDE);

               if (covered_dist < 0.1) // the npc is stuck?

               {

                       my.pan += 90 - random(340); // then add a random angle to its pan angle

                       i = 0;

                       while (i < 80) // walk in the new direction for 10 frames, play with 10

                       {

                               c_move (my, temp.x, nullvector, IGNORE_PASSABLE | GLIDE);

                               ent_animate(my, "walk", my.skill10, ANM_CYCLE); 

                               i++;

                               wait (1);

                       }                                

               }

               else // the npc can move? Then rotate it towards the goal again!

               {

                       vec_set(temp_angle, goal2.x);

                       vec_sub(temp_angle, my.x);

                       vec_to_angle(my.pan, temp_angle);

                       my.tilt = 0;

               }

               if (vec_dist(goal2.x, my.x) < 80) // the npc has found the goal entity?

                       break; // then get out of the while loop!
                  
               wait (1);
                 

       }
goal_tracker();
       // the goal was reached here

//       ent_animate(my, "stand", 0, 0); // switch to "stand"

}


Last edited by rayp; 03/28/14 00:23. Reason: added code tags ^^
Re: NPC follow path A to B back to A [Re: awstar] #407531
09/14/12 20:58
09/14/12 20:58
Joined: Aug 2007
Posts: 1,922
Schweiz
Widi Offline
Serious User
Widi  Offline
Serious User

Joined: Aug 2007
Posts: 1,922
Schweiz
Please use the code tags. Without it is very difficult to read the code.

Re: NPC follow path A to B back to A [Re: Widi] #407537
09/15/12 05:50
09/15/12 05:50
Joined: May 2006
Posts: 30
awstar Offline OP
Newbie
awstar  Offline OP
Newbie

Joined: May 2006
Posts: 30
How do i code tag

Re: NPC follow path A to B back to A [Re: awstar] #407575
09/16/12 01:22
09/16/12 01:22
Joined: Aug 2007
Posts: 1,922
Schweiz
Widi Offline
Serious User
Widi  Offline
Serious User

Joined: Aug 2007
Posts: 1,922
Schweiz
[ code ]
...your code here
[ /code ]

(without the space)

Re: NPC follow path A to B back to A [Re: Widi] #407716
09/18/12 13:28
09/18/12 13:28
Joined: May 2006
Posts: 30
awstar Offline OP
Newbie
awstar  Offline OP
Newbie

Joined: May 2006
Posts: 30
Ok thanks, i actually figured out why it wasnt working and had to remove one "function" word. Ill relist the code shortly as a contribution.


Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1