コード例 #1
0
    public void Move(Vector3 deltaMovement)
    {
        // save off our current grounded state
        bool wasGroundedBeforeMoving = CollisionState.Bottom;

        // clear our state
        CollisionState.Reset();
        _raycastHitsThisFrame.Clear();

        _raycastOrigins.Setup(_transform.position, GetColliderCenter(), GetColliderSize(), SkinWidth);
        RecalculateDistanceBetweenRays();

        // now we check movement in the horizontal dir
        if (!FastMath.Equals(deltaMovement.x, 0f))
        {
            MoveHorizontally(ref deltaMovement);
        }

        if (wasGroundedBeforeMoving && deltaMovement.y <= 0f)
        {
            deltaMovement.y -= 0.01f;
        }

        // next, check movement in the vertical dir
        if (!FastMath.Equals(deltaMovement.y, 0f))
        {
            MoveVertically(ref deltaMovement);
        }

        // move then update our state
        //_transform.Translate(deltaMovement, Space.World);

        // TODO: do we need to move actually? check a length of delta above zero
        _rigidbody.MovePosition(_rigidbody.position + deltaMovement);

        // set our becameGrounded state based on the previous and current collision state
        if (!wasGroundedBeforeMoving && CollisionState.Bottom)
        {
            CollisionState.BecameGroundedThisFrame = true;
        }

        // send off the collision events if we have a listener
        if (CollidedEvent != null)
        {
            for (int i = 0; i < _raycastHitsThisFrame.Count; ++i)
            {
                CollidedEvent(_raycastHitsThisFrame[i]);
            }
        }

        DrawBox();
    }