public void move( Vector3 deltaMovement )
{
// save off our current grounded state which we will use for wasGroundedLastFrame and becameGroundedThisFrame
collisionState.wasGroundedLastFrame = collisionState.below;
// clear our state
collisionState.reset();
_raycastHitsThisFrame.Clear();
_isGoingUpSlope = false;
var desiredPosition = transform.position + deltaMovement;
primeRaycastOrigins( desiredPosition, deltaMovement );
// first, we check for a slope below us before moving
// only check slopes if we are going down and grounded
if( deltaMovement.y < 0 && collisionState.wasGroundedLastFrame )
handleVerticalSlope( ref deltaMovement );
// now we check movement in the horizontal dir
if( deltaMovement.x != 0 )
moveHorizontally( ref deltaMovement );
// next, check movement in the vertical dir
if( deltaMovement.y != 0 )
moveVertically( ref deltaMovement );
// move then update our state
if( usePhysicsForMovement )
{
rigidbody2D.MovePosition( transform.position + deltaMovement );
velocity = rigidbody2D.velocity;
}
else
{
transform.Translate( deltaMovement, Space.World );
// only calculate velocity if we have a non-zero deltaTime
if( Time.deltaTime > 0 )
velocity = deltaMovement / Time.deltaTime;
}
// set our becameGrounded state based on the previous and current collision state
if( !collisionState.wasGroundedLastFrame && collisionState.below )
collisionState.becameGroundedThisFrame = true;
// if we are going up a slope we artificially set a y velocity so we need to zero it out here
if( _isGoingUpSlope )
velocity.y = 0;
// send off the collision events if we have a listener
if( onControllerCollidedEvent != null )
{
for( var i = 0; i < _raycastHitsThisFrame.Count; i++ )
onControllerCollidedEvent( _raycastHitsThisFrame[i] );
}
}