Way-points have always seemed to be an important part of gaming; traversing a unit through a path, patrolling an area, etc. Project and Source downloads are at the bottom of the post.
I started this post with what I thought was a well rounded and defined goal. As I moved forward with the code I thought "Hey, it would be cool to do this and add that". after going around and tripling my scope I decided to pair the features down to the basics: Moving to Random way-points and Traversing a way-point list in order. All other code was shelved for a part 2 and 3 (Reversing Direction, Shortest Paths)
I set forth with the idea to have unique customization events happen to each individual object traversing and with that in mind each unit contains the follow options:
eWaypointAction {TraverseNext, Random}
eWaypointStartAction { TraverseNext, Random }
eWaypointIntermediateAction { TraverseNext, Random }
eWaypointEndAction { Destroy, GoToStart, Random }
eWaypointState { WaitingForPoints, WaitingToStart, Traversing, GoToStart, Paused }
Moving Forward
The "heros" move with the following code:
var directionToNextPoint = _Points[_CurrentPointsIndex] - this.transform.position;
var directionToNextPointNormal = directionToNextPoint.normalized;
this.transform.position += directionToNextPointNormal * _TravelSpeed * Time.smoothDeltaTime;
While the 'Moving Forward' code is in no way perfect, it works pretty solid for this application and should be quite easy to follow.Random way-points
EnterStartPoint = Random
EnterIntermediatePoint = Random
EnterEndPoint = Random
chosing a random point is pretty easy, I just need to call:
_CurrentPointsIndex = Random.Range(0, _Points.Length);
Later I would like to add some intelligence and track already visited way-points to avoid them and keep a good coverage on the board.
Traverse way-points
EnterStartPoint = TraverseNext
EnterIntermediatePoint = TraverseNext
EnterEndPoint = TraverseNext
Traversing is simple, just increment the current index. with the EnterStartPoints and EnterEndPoint I can call custom actions when my hero reaches the respective positions.
A Tower defense might call a damage event and destroy, a patrolling unit might change states whenever he reaches the end. options are endless
Downloads
Source code: TraverseWaypoints.cs
Comments
Post a Comment