'Ursina engine. How to make cubes not appear on other cubes

So I wanted to something like mining game in ursina engine. I wanted that if you mine a block other blocks will appear under that block and on other sides of this block creating a mining effect. I tried raycasting but i can't properly set it and it seems that i even don't understand raycasting. My inspiration is Mining Simulator game from roblox(maybe that help to understand what am i trying to achive. Here is a link to this game: https://www.roblox.com/games/1417427737/Mining-Simulator). Basicly i want to check if there isnt a block beside and if there is not a block beside then dont generate a block. Here's my code:

from ursina import *
from ursina.prefabs.first_person_controller import FirstPersonController
app = Ursina(borderless=False)
g = Entity(model='plane', scale = (16,1,16), collider='box')
Sky()
player = FirstPersonController(jump_height=3)
class Voxel(Button):
    global depth
    def __init__(self, position = (0,0,0)):
        super().__init__(
            parent = scene,
            position = position,
            model = 'cube',
            collider = 'box',
            rotation = (0,0,0),
            scale = (3,3,3),
            color = color.rgb(random.uniform(0,255),random.uniform(0,255),random.uniform(0,255))
            )
    def input(self, key):
        def gen_blocks():
            p = self.position
            p.y -= 3
            voxel = Voxel(position=p)
            p.y += 3
            p.z -= 3
            voxel = Voxel(position=p)
            p.z += 6
            voxel = Voxel(position=p)
            p.z -= 3
            p.x += 3
            voxel = Voxel(position=p)
            p.x -= 6
            voxel = Voxel(position=p)
        if self.hovered:
            if key == 'left mouse down':
                gen_blocks()
                destroy(self)
for z in range(8):
        for x in range(8):
            voxel = Voxel(position = (x * 3 - 8,-1,z * 3 + 9))
app.run()


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source