/// <summary> /// attempts to move the character to position + deltaMovement. Any colliders in the way will cause the movement to /// stop when run into. /// </summary> /// <param name="deltaMovement">Delta movement.</param> 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; primeRaycastOrigins(); // first, we check for a slope below us before moving // only check slopes if we are going down and grounded if (deltaMovement.y < 0f && collisionState.wasGroundedLastFrame) { handleVerticalSlope(ref deltaMovement); } // now we check movement in the horizontal dir if (deltaMovement.x != 0f) { moveHorizontally(ref deltaMovement); } // next, check movement in the vertical dir if (deltaMovement.y != 0f) { moveVertically(ref deltaMovement); } // move then update our state deltaMovement.z = 0; transform.Translate(deltaMovement, Space.World); // only calculate velocity if we have a non-zero deltaTime if (Time.deltaTime > 0f) { 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]); } } ignoreOneWayPlatformsThisFrame = false; }
public void move(Vector3 deltaMovement) { // save off our current grounded state var wasGroundedBeforeMoving = collisionState.below; // clear our state collisionState.reset(); _raycastHitsThisFrame.Clear(); primeRaycastOrigins(); // first, we check for a slope below us before moving // only check slopes if we are going down and grounded if (deltaMovement.y < 0 && wasGroundedBeforeMoving) { 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) { #if UNITY_4_5 || UNITY_4_6 rigidbody2D.MovePosition(transform.position + deltaMovement); #else rigidbody2D.velocity = deltaMovement / Time.fixedDeltaTime; #endif 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 (!wasGroundedBeforeMoving && collisionState.below) { collisionState.becameGroundedThisFrame = true; } // send off the collision events if we have a listener if (onControllerCollidedEvent != null) { for (var i = 0; i < _raycastHitsThisFrame.Count; i++) { onControllerCollidedEvent(_raycastHitsThisFrame[i]); } } }