private void moveVertically( ref Vector3 deltaMovement )
{
var isGoingUp = deltaMovement.y > 0;
var rayDistance = Mathf.Abs( deltaMovement.y ) + _skinWidth;
var rayDirection = isGoingUp ? Vector2.up : -Vector2.up;
var initialRayOrigin = isGoingUp ? _raycastOrigins.topLeft : _raycastOrigins.bottomLeft;
// apply our horizontal deltaMovement here so that we do our raycast from the actual position we would be in if we had moved
initialRayOrigin.x += deltaMovement.x;
// if we are moving up, we should ignore the layers in oneWayPlatformMask
var mask = platformMask;
if( isGoingUp && !collisionState.wasGroundedLastFrame )
mask &= ~oneWayPlatformMask;
for( var i = 0; i < totalVerticalRays; i++ )
{
var ray = new Vector2( initialRayOrigin.x + i * _horizontalDistanceBetweenRays, initialRayOrigin.y );
DrawRay( ray, rayDirection * rayDistance, Color.red );
_raycastHit = Physics2D.Raycast( ray, rayDirection, rayDistance, mask );
if( _raycastHit )
{
// set our new deltaMovement and recalculate the rayDistance taking it into account
deltaMovement.y = _raycastHit.point.y - ray.y;
rayDistance = Mathf.Abs( deltaMovement.y );
// remember to remove the skinWidth from our deltaMovement
if( isGoingUp )
{
deltaMovement.y -= _skinWidth;
collisionState.above = true;
}
else
{
deltaMovement.y += _skinWidth;
collisionState.below = true;
}
_raycastHitsThisFrame.Add( _raycastHit );
// this is a hack to deal with the top of slopes. if we walk up a slope and reach the apex we can get in a situation
// where our ray gets a hit that is less then skinWidth causing us to be ungrounded the next frame due to residual velocity.
if( !isGoingUp && deltaMovement.y > 0.00001f )
_isGoingUpSlope = true;
// we add a small fudge factor for the float operations here. if our rayDistance is smaller
// than the width + fudge bail out because we have a direct impact
if( rayDistance < _skinWidth + kSkinWidthFloatFudgeFactor )
return;
}
}
}