Can't get current position of the player in ursina

43 Views Asked by At

I am building a game in ursina, python and I am not able to get the current position of the player.

Here is my code:

from ursina import *
from ursina.prefabs.first_person_controller \
    import FirstPersonController
from os import listdir

textures = listdir("assets/texture/")
app = Ursina()
dict_textures = {
    index + 1 : {
        "name":    file.replace(".png", "_texture"),
        "texture": load_texture(file)
    }
    for index, file in enumerate(textures)
}
sky_texture = load_texture("/assets/Sky.png")

def input(key):
    if key == "escape":
        exit()
    elif key == 'r':
        position = player.position
        print(position)

class Sky(Entity):
    def __init__(self):
        super().__init__(
            parent=scene,
            model="sphere",
            texture=sky_texture,
            scale = 5000,
            double_sided = True)

class Grass_Block(Button):
    def __init__(
            self,
            position,
            texture = load_texture(
                "/assets/texture/Grass_Block.png"
            )
        ):
        super().__init__(
            parent=scene,
            position=position,
            model="cube",
            origin_y=0.5,
            texture=texture,
            color=color.white,
            colision = "mesh",
            double_sided = True)

def create_map():
    with open("maze.txt", "r") as file:
        map = file.read()
    map = map.split("\n")
    map = [list(line) for line in map]

    for z in range(len(map)):
        for x in range(len(map[0])):
            _ = Grass_Block(position=(z, 0, x))
            if map[z][x] == '1':
                for i in range(5):
                    _ = Grass_Block(
                        position=(z, i+1, x),
                        texture="/assets/texture/Mushroom_dirt.png"
                    )

class Player(Entity):
    def __init__(self):
        super().__init__()
        self.controller = FirstPersonController(
            mouse_sensitivity = Vec2(100, 100),
            player = self
        )

if __name__ == "__main__":
    create_map()
    player = Player()
    sky = Sky()
    app.run()

Every time I enter the key R will print Vec3(0,0,0) but not the current position. Create map used to build the walls, of the maze and the floor on which the player should walk. Because the player is set to (0,0,0) position he spawn inside the walls. I can actually exit the walls and sit on them by jumping using space bar, but I would like also to set initial position. Seems like the position attribute of the player doesn't update and can't be changed. enter image description here

What am I doing wrong ?

1

There are 1 best solutions below

0
On

I removed the Player class and used this instead:

if __name__ == "__main__":
    player = FirstPersonController(
        speed=10,
        jump_height=1
    )
    player.position = (1,0,1)
    create_map()
    sky = Sky()
    app.run()