Beispiel #1
0
    // find out if the object is grounded
    bool isGrounded()
    {
        // for position, get the "bottom" edge in terms of the direction of gravity
        Vector2 position = (Vector2)transform.position + PhysicsUtilities.OffsetToEdgeRenderer(rend);
        //Debug.DrawLine(transform.position, position);
        float        dist = 0.1f;
        RaycastHit2D hit  = PhysicsUtilities.RaycastToGravity(position, dist, GameController.terrainLayer);

        if (hit.collider != null)
        {
            return(true);
        }

        return(false);
    }
    // check if we're grounded
    bool isGrounded()
    {
        Vector2 position = _groundChecker.position; // get our groundChecker object
        float   dist     = 0.1f;                    // how far are we checking

        // check both ends and the center of the game object
        RaycastHit2D[] hits = new RaycastHit2D[] {
            PhysicsUtilities.RaycastToGravity(position - new Vector2(_rend.bounds.size.x / 2f, 0), dist, GameController.terrainLayer),
            PhysicsUtilities.RaycastToGravity(position, dist, GameController.terrainLayer),
            PhysicsUtilities.RaycastToGravity(position + new Vector2(_rend.bounds.size.x / 2f, 0), dist, GameController.terrainLayer)
        };

        foreach (RaycastHit2D hit in hits)
        {
            if (hit.collider != null)
            {
                GameController.gravTransitionState = false; // if we hit ground, we're not in GameController.gravTransitionState
                return(true);
            }
        }

        return(false);
    }