示例#1
0
    private void CheckSurroundings()
    {
        // ---- CHECK FOR GROUND

        RaycastHit2D onGround = Physics2D.Raycast(onGroundCheck.position, Vector2.down, groundCheckDistance, groundLayerMask);

        Debug.DrawRay(onGroundCheck.position, Vector2.down * groundCheckDistance, Color.red);

        RaycastHit2D toeHitGround = Physics2D.Raycast(toeGroundCheck.position, Vector2.down, groundCheckDistance, groundLayerMask);

        Debug.DrawRay(toeGroundCheck.position, Vector2.down * groundCheckDistance, Color.yellow);

        RaycastHit2D heelHitGround = Physics2D.Raycast(heelGroundCheck.position, Vector2.down, groundCheckDistance, groundLayerMask);

        Debug.DrawRay(heelGroundCheck.position, Vector2.down * groundCheckDistance, Color.yellow);

        if (onGround.collider != null)
        {
            isOnGround = true;

            int currentLayer = onGround.collider.gameObject.layer;

            if (currentLayer == breakableObjectsLayer)
            {
                BreakableObject bo = onGround.collider.gameObject.GetComponent <BreakableObject>();

                if (bo == null)
                {
                    Debug.LogError("Object on Breakable Objects layer does not have Breakable Object component!");
                }
                else if (bo.isPlatform && !bo.isFallingApart)
                {
                    bo.TriggerPlatformCollapse();
                }
            }
            else if (currentLayer == breakableFloorsLayer)
            {
                BreakableFloor bf = onGround.collider.gameObject.GetComponent <BreakableFloor>();

                if (bf == null)
                {
                    Debug.LogError("Object on Breakable Floors layer does not have Breakable Floors component!");
                }
                else if (inAir)
                {
                    bf.TriggerObjectShake();
                }
            }
            else if (currentLayer == slidingSurfaceLayer && !isPressingJumpButton && !magBootsOn)
            {
                SlidingSurface ss = onGround.collider.gameObject.GetComponent <SlidingSurface>();

                if (ss == null)
                {
                    Debug.LogError("Object on Sliding Surface layer does not have Sliding Surface component!");
                }
                else
                {
                    SlopeSlide(ss.direction);
                }
            }
            else if (currentLayer != slidingSurfaceLayer && isGroundSliding || isGroundSliding && magBootsOn)
            {
                print("Stop");
                StopSlopeSlide();
            }
        }
        else if (onGround.collider == null && isGroundSliding)
        {
            if (direction == Vector2.right)
            {
                move.x = 16;
            }
            else
            {
                move.x = -16;
            }

            targetVelocity.x = move.x;
        }
        else if (onGround.collider == null)
        {
            if (toeHitGround.collider != null && heelHitGround.collider == null && !hasJumped)
            {
                move.x = -3f;
            }
            else if (toeHitGround.collider == null && heelHitGround.collider != null && !hasJumped)
            {
                move.x = 3f;
            }
            else if (toeHitGround.collider == null && heelHitGround.collider == null)
            {
                isOnGround = false;
            }
        }

        // ---- CHECK FOR WALLS

        if (canMove && !isDisabled)
        {
            ContactFilter2D contactFilter = new ContactFilter2D();
            contactFilter.SetLayerMask(wallLayerMask);
            RaycastHit2D[] hit = new RaycastHit2D[1];
            isTouchingWall = Physics2D.Raycast(wallCheck.position, direction, contactFilter, hit, wallCheckDistance) > 0;
            Debug.DrawRay(wallCheck.position, direction * wallCheckDistance, Color.red);

            isTouchingLedge = Physics2D.Raycast(ledgeCheck.position, direction, ledgeCheckDistance, wallLayerMask);
            Debug.DrawRay(ledgeCheck.position, direction * ledgeCheckDistance, Color.red);

            // Check for ledge grab
            if (isTouchingWall && !isTouchingLedge && !ledgeDetected)// << ----------- OPTION TO PUT MANUAL LEDGE GRAB HERE
            {
                ledgeDetected = true;
                // playerposition = hitpointx - wallcheck
                transform.position = new Vector3(hit[0].point.x + (direction == Vector2.right ? wallCheckDistance : -wallCheckDistance), transform.position.y, transform.position.z);
                ledgePosBot        = wallCheck.position;
            }

            // Check for crumbling wall
            if (isWallSliding)
            {
                RaycastHit2D hitWall = Physics2D.Raycast(wallCheck.position, direction, wallCheckDistance, groundLayerMask);

                if (hitWall.collider != null)
                {
                    int currentLayer = hitWall.collider.gameObject.layer;

                    if (currentLayer == breakableObjectsLayer)
                    {
                        BreakableObject crumblingWall = hitWall.collider.gameObject.GetComponent <BreakableObject>();

                        if (crumblingWall != null)
                        {
                            print("hit crumbling wall");
                            crumblingWall.TriggerPlatformCollapse();
                            StopWallSliding();
                        }
                        else
                        {
                            Debug.LogError("Object on Breakable Object layer does not have Breakable Object component!");
                        }
                    }
                }
            }
        }
    }