if (!_ReachedDestination)
{
// I'm not sure why you need to subtract the TranslationVector from the Waypoint which is the point you need to go to in world space?
var Direction = _Waypoint - Unit.Transform.WorldMatrix.TranslationVector;
// I'm not sure why you would divide the length here for the direction?
// Get distance towards next point and normalize the direction at the same time
var LengthToDestination = Direction.Length();
Direction /= LengthToDestination;
// Check when to advance to the next waypoint
bool WaypointAdvance = false;
// Check to see if an intermediate point was passed by projecting the position along the path
if (_PathToDestination.Count > 0 && _WaypointIndex > 0 && _WaypointIndex != _PathToDestination.Count - 1)
{
Vector3 PointNormal = _Waypoint - _PathToDestination[_WaypointIndex - 1];
PointNormal.Normalize();
// I think the Unit part is where the unit is in world space so we are doing a dot method to find the distance from our waypoint?
float Current = Vector3.Dot(Unit.Transform.WorldMatrix.TranslationVector, PointNormal);
float Target = Vector3.Dot(_Waypoint, PointNormal);
// If we are at our waypoint or passed it advance to the next waypoint?
if (Current > Target)
{
WaypointAdvance = true;
}
}
else
{
// Check distance to final point
if (LengthToDestination < _DestinationThreshold)
{
WaypointAdvance = true;
}
}
// Advance waypoint?
if (WaypointAdvance)
{
_WaypointIndex++;
if (_ReachedDestination)
{
// Final waypoint reached
Stop(_ListUnit);
return;
}
}
// Calculate speed based on distance from final destination
// Slows the unit down or speeds it up...? based on how far away from the end point it is?
float moveSpeed = (_MoveDestination - Unit.Transform.WorldMatrix.TranslationVector).Length() * _DestinationSlowdown;
if (moveSpeed > 1.0f)
{
moveSpeed = 1.0f;
}
// Slow down around corners
// I know this puts an arc in the path but i dont understand why you would want that for a straight line on my 3d plane it still arcs a bit to its destination
// I need to figure otu how to make it go straight if there are no corners
float cornerSpeedMultiply = Math.Max(0.0f, Vector3.Dot(Direction, _MoveDirection)) * _CornerSlowdown + (1.0f - _CornerSlowdown);
// Allow a very simple inertia to the character to make animation transitions more fluid
// Adds everything up to try to provide a direction on the update game loop
_MoveDirection = _MoveDirection * 0.85f + Direction * moveSpeed * cornerSpeedMultiply * 0.15f;
// Using the default character component to do the moving
_CharacterComponent.SetVelocity(_MoveDirection * _Speed);
// Make the unit face the direction its traveling but at the end when it gets to its end point rotates back to the -z axis... not sure why it reverts to facing that way instead of just facing the way it was going
if (_MoveDirection.Length() > 0.001)
{
_YawOrientation = MathUtil.RadiansToDegrees((float)Math.Atan2(-_MoveDirection.Z, _MoveDirection.X) + MathUtil.PiOverTwo);
}
Unit.Transform.Rotation = Quaternion.RotationYawPitchRoll(MathUtil.DegreesToRadians(_YawOrientation), 0, 0);
}
else
{
Stop(_ListUnit);
}
}
Below is some code I pulled from our current project. I'm trying to figure out how in the world it works. Can someone take a look at my comments and tell me if its right or shine some light on the question I commented out?