NPC Path
De DigiWiki.
Part 1 - Path creation script:
Drop this script in a prim, wear it on your HUD. Start walking! Just click it every time you want your NPC to move to that spot. So click at point A go a ways, click point B, then C, etc. Then it will path along those points.
Currently this is written so you can keep on clicking then type in '/12 anything' and it will create the notecard 'Path1'. Just copy that into inventory and go on to the next step
vector path_pos; string path_write; string notename = "Path1"; default { state_entry() { llListen(12,"",llGetOwner(),""); } touch_start(integer n) { path_pos = llDetectedPos(0); path_write += (string)path_pos+"\n"; } listen(integer channel, string name, key id, string msg) { if (msg != "") { osMakeNotecard(notename,path_write); llOwnerSay("Notecard Created"); } } }
Part 2 - NPC path script:
This is written to currently take a notecard named 'Path1' as previously stated. You can rename it however you like. It is one vector per line, as many vectors as you want. The NPC is spawned at the first vector in the notecard. Once you type /10 start - it is off to the races. Please note that it is currently setup to use an animation named 'walk' please use whatever accordingly as the script will of course error out if it cannot find it.
Remember that notecard 'Path1' we put into inventory?? Time to drop it into this same prim or the script won't be happy
key npc; vector curPos; vector vecTo_targ; vector nPos; list path; integer path_length; integer pad_Turn = 3; // Larger the number smoother the turn but less accurate string notecard; // Smaller the number more accurate but sharper noticeable stop/turn default { state_entry() { llListen(10,"",NULL_KEY,""); integer i; notecard = "Path1"; // Name of path vector notecard for(i=1; i<osGetNumberOfNotecardLines(notecard); i++) { path += osGetNotecardLine(notecard, i); } // Grab path vectors from notecard and pack into list path_length=llGetListLength(path); } touch_start(integer num) { llOwnerSay("\n\nNPC Path Block:\n Type /10 (command) to control your NPC.\n create - spawn NPC\n start - start NPC path\n stop - stop and remove the NPC"); } listen(integer channel, string name, key id, string msg) { if (msg != "") { if (msg == "create") { osOwnerSaveAppearance("appearance"); vector spawn = (vector)osGetNotecardLine(notecard, 1); npc = osNpcCreate ("Justa", "Walkin", spawn, "appearance"); } else if (msg == "start") { integer x; for (x = 1; x < path_length; x++) { vecTo_targ = llList2Vector(path,x); nPos = osNpcGetPos(npc); if (vecTo_targ.x > 1) { osAvatarPlayAnimation(npc,"walk"); osNpcSetRot(npc, llRotBetween(<PI,PI,0>,llVecNorm(vecTo_targ - osNpcGetPos(npc)))); osNpcMoveToTarget(npc, vecTo_targ, OS_NPC_NO_FLY); while (llFabs(llVecDist(vecTo_targ, nPos)) > pad_Turn) { nPos = osNpcGetPos(npc); llSleep(.5); } // Probably a lower lag way to do this with states or something // This also locks out the script until the loop is complete // Which I really really hate! :P } else { llSleep(.5); // Stop animation buffer // no duck walking last couple of feet // adjust for animation or make a variable osAvatarStopAnimation(npc,"walk"); } } } else if (msg == "stop") { osNpcRemove(npc); llResetScript(); // Stop all processes. Kill all NPCs. } else { llOwnerSay("Unkown Command"); } } } }