Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Ursina Engine Cube is not appearing under cube upon clicking

So i have my code and I want to appear a cube under a cube i will click. Then i want to check if there are cubes around this cube if in some place there isnt a cube then create one but i cant do this. Can someone help me?

from ursina import *
from ursina.prefabs.first_person_controller import FirstPersonController
app = Ursina(borderless = False)

player = FirstPersonController(model='none', position=(0,5,0), scale=2)


Sky()

overworld_ground = Entity(
    model = 'cube',
    scale = (32,2,18),
    position = (0,0,0),
    collider = 'mesh',
    color = color.rgb(0, 128, 0),
    texture = 'grass'
    )
class Voxel(Button):
    def __init__(self, position = (0,0,0)):
        super().__init__(
            parent = scene,
            position = self.position,
            model = 'cube',
            collider = 'box',
            rotation = (0,0,0),
            color = color.rgb(0, random.uniform(162,182), 0),
            scale = (2, 2, 2))
    def input(self, key):
        if self.hovered:
            if key == 'left mouse down':
                voxel = Voxel(self.position.y - 2)
                destroy(self)

for z in range(8):
        for x in range(8):
            voxel = Voxel(position = (x * 2 - 11,-1,z * 2 + 10))



app.run()

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I see two problems with your code. When I fix both of these, I get something reasonable that I think will put you in a place where you can proceed with your development:

  1. I get an error when I try to run your code, involving the line position = self.position,. This line makes no sense. Here, you reference the position attribute of the object that you’re just starting to construct. Also, you aren’t using the position parameter passed to the constructor. To resolve both of these issues, I expect that what you want here is position = position to simply pass the passed in position along to the superclass’s constructor.

  2. This line looks suspicious as well: voxel = Voxel(self.position.y - 2). You’re taking just the y component of an object’s position and passing that to the Voxel constructor that expects a 3-d position.

I fixed both of these issues, and played around with where the new Voxels are created so that I could see them. Once I did this, clicking on an existing Voxel caused a new one to appear and that one to disappear, which seems like what you want to see happening.

So here’s my fixed version of your code that behaves more "properly":

from ursina import *
from ursina.prefabs.first_person_controller import FirstPersonController
app = Ursina(borderless = False)

player = FirstPersonController(model='none', position=(0,5,0), scale=2)


Sky()

overworld_ground = Entity(
    model = 'cube',
    scale = (32,2,18),
    position = (0,0,0),
    collider = 'mesh',
    color = color.rgb(0, 128, 0),
    texture = 'grass'
    )
class Voxel(Button):

    def __init__(self, position = (0,0,0)):
        super().__init__(
            parent = scene,
            position = position,  # <- was self.position
            model = 'cube',
            collider = 'box',
            rotation = (0,0,0),
            color = color.rgb(0, random.uniform(162,182), 0),
            scale = (2, 2, 2))

    def input(self, key):
        if self.hovered:
            if key == 'left mouse down':
                p = self.position
                p.z += 20
                voxel = Voxel(position=p)
                destroy(self)

for z in range(8):
        for x in range(8):
            voxel = Voxel(position = (x * 2 - 11,-1,z * 2 + 10))

app.run()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading