Hello
How to create a character following a pre-fabricated path at a variable speed?
thanks
How to create a character following a pre-fabricated path at a variable speed?
thanks
Dim waypoint(100,2) ; 101 way points 2= x,y and z
Type waypoint Field x,y,z End Type
w.waypoint=new waypoint w\x=100 w\y=100 w\z=10
Graphics3D 800,600 SetBuffer BackBuffer() SeedRnd MilliSecs() Global cam=CreateCamera() PositionEntity cam,0,7,-20 light=CreateLight() ; SET UP SOME CUBES, JUST FOR SCENARY PURPOSES, TO HIGHLIGHT THE MOVEMENT Dim cubes(10) For loop=0 To 10 cubes(loop)=CreateCube() PositionEntity cubes(loop),Rand(-15,15),1,Rand(-17,17) EntityAlpha cubes(loop),0.2 Next ;/ END OF CUBES SET UP ; CREATE THE 'PLAYER' Global sphere=CreateSphere() ; SET THE NUMBER OF WAYPOINTS YOU WANT Global no_of_waypoints=10 ; number of waypoint you want, - 1 ; SET UP THE WAYPOINT DATA Dim waypoints#(no_of_waypoints,3); the 3 is for X Y & Z co-ords and the speed ; 0 = x ; 1 = y ; 2 = z ; 3 = speed waypoint_data(no_of_waypoints); get the waypoint positions and speed ; SET UP THE WAYPOINT MARKERS. SIMPLY A VISUAL REPRESENTATION OF WHERE THE WAYPOINTS ARE, NOTHING MORE Dim waypoint_markers(no_of_waypoints); visible representations of the waypoints, purely for clarity For loop=0 To no_of_waypoints waypoint_markers(loop)=CreateSphere(2) EntityAlpha waypoint_markers(loop),0 EntityColor waypoint_markers(loop),50,50,200 Next position_markers(no_of_waypoints); position the waypoint markers ; CREATE THE PIVOT THAT THE PLAYER WILL GOTO. THIS PIVOT IS SET TO THE CURRENT WAYPOINT POSITION, WHICH ; THE PLAYER POINTS TO AND FOLLOWS Global waypoint_pivot=CreatePivot() Global waypoint_number=0 ; what waypoint the program will start on While Not KeyHit(1) If KeyDown(2) Then ; IF KEY 1 IS PRESSED THEN show_waypoints() ; SHOW THE WAY POINTS End If update_waypoints(waypoint_number); UPDATE THE WAYPOINTS AND THE PLAYER FOLLOWING THEM PointEntity cam,sphere; POINT THE CAMERA AT THE SPHERE UpdateWorld RenderWorld Flip Wend End Function show_waypoints() For loop=0 To no_of_waypoints EntityAlpha waypoint_markers(loop),0.6 Next End Function Function waypoint_data(number) ; Set random waypoint positions and a random speed for each waypoint For loop=0 To number waypoints(loop,0)=Rnd(-20,20) ; x waypoints(loop,1)=Rnd(-7,9) ; y waypoints(loop,2)=Rnd(-20,20) ; z waypoints(loop,3)=Rnd(0.1,0.5) ; random speed Next End Function Function position_markers(number); simply position the markers at the waypoint locations For loop=0 To number PositionEntity waypoint_markers(loop),waypoints(loop,0),waypoints(loop,1),waypoints(loop,2); position the way point markers Next End Function Function update_waypoints(waypoint_no) ; update the player, so he follows each waypoint ; Line Below: position the waypoint pivot at the location of the current waypoint PositionEntity waypoint_pivot,waypoints(waypoint_no,0),waypoints(waypoint_no,1),waypoints(waypoint_no,2) PointEntity sphere,waypoint_pivot; Point the sphere (player) at the current waypoint location MoveEntity sphere,0,0,waypoints(waypoint_no,3); move the sphere (player) towards the current way-point ; using the current way-points speed If EntityDistance#(sphere,waypoint_pivot)<2 Then ; if the sphere (player) comes pretty close to the waypoint ; within 2 units, then... waypoint_number=waypoint_number+1 ; move to the next way-point If waypoint_number > no_of_waypoints Then ; if the waypoint is past the last way-point then... waypoint_number=0 ; loop it back to the very first waypoint End If End If End Function