コード例 #1
0
    private void Backtrack(BoxRayCaster.RayTrigger trigger)
    {
        Vector2       rayDirection    = trigger.RayDirection;
        BoxCollider2D box             = boxRayCaster.boxCollider2D;
        Vector2       nowWorldCenter  = transform.TransformPoint(box.offset);
        Vector2       rayAbsDirection = new Vector2(Mathf.Abs(rayDirection.x), Mathf.Abs(rayDirection.y));
        Vector2       lastWorldCenter = nowWorldCenter - Vector2.Scale(Velocity, rayAbsDirection) * UpdateTime;
        float         step            = trigger.RayStep;
        float         length          = 100;
        ThreeRayCast  threeRayCast    = new ThreeRayCast(lastWorldCenter, rayDirection, step, length, backtrackLayer);
        RaycastHit2D? result          = threeRayCast.GetNearest();

        Debug.Assert(result.HasValue);
        Vector2 hitPoint   = result.Value.point;
        float   deltaValue = trigger.GetWorldDirectionValue(lastWorldCenter, hitPoint);

        //deltaValue += backtrackWidth;
        if (trigger.GetNeedVector2Value(Velocity) != 0)
        {
            //Vector2 nextWorldCenter = lastWorldCenter + deltaValue / trigger.GetNeedVector2Value(Velocity) * Velocity;
            Vector2 nextWorldCenter = lastWorldCenter + deltaValue * rayAbsDirection;
            Position += nextWorldCenter - nowWorldCenter;
            if (rayDirection.x == 0)
            {
                Velocity = new Vector2(Velocity.x, 0);
            }
            else
            {
                Velocity = new Vector2(0, Velocity.y);
            }
        }
    }
コード例 #2
0
        void SetRayValue()
        {
            if (_direction == Direction.DOWN)
            {
                rayCenter    = _parent.boxCollider2D.offset - _parent.boxCollider2D.size / 2;
                rayCenter.x  = _parent.boxCollider2D.offset.x;
                rayCenter   += Vector2.up * _parent.rayLength;
                rayDirection = Vector2.down;
                rayStep      = _parent.boxCollider2D.size.x / 2;
            }
            else if (_direction == Direction.LEFT)
            {
                rayCenter    = _parent.boxCollider2D.offset;
                rayCenter.x  = rayCenter.x - _parent.boxCollider2D.size.x / 2;
                rayCenter   += Vector2.right * _parent.rayLength;
                rayDirection = Vector2.left;
                rayStep      = _parent.boxCollider2D.size.y / 2;
            }
            else if (_direction == Direction.RIGHT)
            {
                rayCenter    = _parent.boxCollider2D.offset;
                rayCenter.x  = rayCenter.x + _parent.boxCollider2D.size.x / 2;
                rayCenter   += Vector2.left * _parent.rayLength;
                rayDirection = Vector2.right;
                rayStep      = _parent.boxCollider2D.size.y / 2;
            }
            else if (_direction == Direction.UP)
            {
                rayCenter    = _parent.boxCollider2D.offset + _parent.boxCollider2D.size / 2;
                rayCenter.x  = _parent.boxCollider2D.offset.x;
                rayCenter   += Vector2.down * _parent.rayLength;
                rayDirection = Vector2.up;
                rayStep      = _parent.boxCollider2D.size.x / 2;
            }
            rayCenter += rayDirection * _parent.rayLength / 2;
            rayStep   -= _parent.rayStepOffSet;
            Quaternion rotation       = _parent.transform.rotation;
            Vector3    rayDirectionV3 = new Vector3(rayDirection.x, rayDirection.y, 0);

            rayDirectionV3 = rotation * rayDirectionV3;
            rayDirection   = new Vector2(rayDirectionV3.x, rayDirectionV3.y);
            threeRayCast   = new ThreeRayCast(rayCenter, rayDirection, rayStep, _parent.rayLength, _parent.rayLayer, _parent.transform);
        }