Exemplo n.º 1
0
    //通过封装的raycast方法,检测射线是否碰撞到地面。并通过set方法通知movementplayer。
    override public void MyUpdate()
    {
        //下方探测器
        RaycastHit2D floorLeftCheck  = Raycast(floorLeftRay, Vector2.down, floorDetectorLength, groundLayer);
        RaycastHit2D floorRightCheck = Raycast(floorRightRay, Vector2.down, floorDetectorLength, groundLayer);

        if (floorLeftCheck || floorRightCheck)
        {
            playerMovementComponent.SetOnFloor(gameObject, true);

            //设置父物体
            if (floorLeftCheck && floorLeftCheck.collider.gameObject.tag == "MoveablePlatform")
            {
                player.transform.SetParent(floorLeftCheck.collider.gameObject.transform);
            }
            else if (floorRightCheck && floorRightCheck.collider.gameObject.tag == "MoveablePlatform")
            {
                player.transform.SetParent(floorRightCheck.collider.gameObject.transform);
            }
            else if (player.gameObject.transform.parent != null)
            {
                player.gameObject.transform.parent = null;
            }

            //如果碰到地板而且在下落状态
            //if (playerMovementComponent.GetYSpeed() < 0)
            //{
            //取hit中最大的那个(如果只有一个探针碰到则另一个distance = 0)进行移动
            float maxDistance = floorLeftCheck.distance > floorRightCheck.distance ? floorLeftCheck.distance : floorRightCheck.distance;
            playerMovementComponent.SetFloorOffset(floorDetectorAsecendDistance - maxDistance);
            //}
        }
        else
        {
            playerMovementComponent.SetOnFloor(gameObject, false);
        }

        //左方探测器
        RaycastHit2D leftTopCheck  = Raycast(leftTopRay, Vector2.left, LRDetectorTotalLength, groundLayer);
        RaycastHit2D leftDownCheck = Raycast(leftDownRay, Vector2.left, LRDetectorTotalLength, groundLayer);

        if (leftDownCheck || leftTopCheck)
        {
            isTouchingLeftGround = true;
            playerMovementComponent.SetLeftDetect(gameObject, true);
            if (playerMovementComponent.GetXSpeed() < 0)
            {
                Debug.Log("左侧x轴距离补偿");
                //取hit中最大的那个(如果只有一个探针碰到则另一个distance = 0)进行移动
                float maxDistance = leftTopCheck.distance > leftDownCheck.distance ? leftTopCheck.distance : leftDownCheck.distance;
                playerMovementComponent.SetRightOffset(LRDetectorInsideLength - maxDistance);
            }
        }
        else
        {
            playerMovementComponent.SetLeftDetect(gameObject, false);
            isTouchingLeftGround = false;
        }

        //右方探测器
        RaycastHit2D rightTopCheck  = Raycast(rightTopRay, Vector2.right, LRDetectorTotalLength, groundLayer);
        RaycastHit2D rightDownCheck = Raycast(rightDownRay, Vector2.right, LRDetectorTotalLength, groundLayer);

        if (rightDownCheck || rightTopCheck)
        {
            isTouchingRightGround = true;
            playerMovementComponent.SetRightDetect(gameObject, true);
            //if (playerMovementComponent.GetXSpeed() > 0)
            //{
            //Debug.Log("右侧侧x轴距离补偿");
            //取hit中最大的那个(如果只有一个探针碰到则另一个distance = 0)进行移动
            float maxDistance = rightTopCheck.distance > rightDownCheck.distance ? rightTopCheck.distance : rightDownCheck.distance;
            playerMovementComponent.SetRightOffset(maxDistance - LRDetectorInsideLength);
            //}
        }
        else
        {
            isTouchingRightGround = false;
            playerMovementComponent.SetRightDetect(gameObject, false);
        }

        //旧输入系统
        if (rightTopCheck && Input.GetAxis("Horizontal") > 0f)
        {
            playerMovementComponent.SetSideWall(2);
        }
        else if (leftTopCheck && Input.GetAxis("Horizontal") < 0f)
        {
            playerMovementComponent.SetSideWall(1);
        }
        //if (rightTopCheck && keyboard.dKey.isPressed)
        //{
        //    playerMovementComponent.SetSideWall(2);
        //}
        //else if (leftTopCheck && keyboard.aKey.isPressed)
        //{
        //    playerMovementComponent.SetSideWall(1);
        //}
        else
        {
            playerMovementComponent.SetSideWall(0);
        }
        //上方探测器
        RaycastHit2D headLeftCheck  = Raycast(headLeftRay, Vector2.up, headDetectorLength, groundLayer);
        RaycastHit2D headRightCheck = Raycast(headRightRay, Vector2.up, headDetectorLength, groundLayer);

        if (headLeftCheck || headRightCheck)
        {
            playerMovementComponent.SetDownFloor(gameObject, true);
        }
        else
        {
            playerMovementComponent.SetDownFloor(gameObject, false);
        }

        RaycastHit2D halfInWater = Raycast(halfInWaterRay, Vector2.down, waterDetectorLength, waterLayer);

        if (halfInWater)
        {
            playerMovementComponent.SetInWater(true);
            //Debug.Log("进入水中");
        }
        else
        {
            playerMovementComponent.SetInWater(false);
        }
    }