繁体   English   中英

当我在 ursina 中向播放器添加相机时,我的播放器正在下降并沿其 y 轴移动

[英]my player is falling and moving on its y axis when i add a camera to the player in ursina

好的,在我的代码中,我试图让相机跟随我的播放器,并且我正在关注我在堆栈溢出中找到的教程。 但是当我这样做时,玩家“跌倒”并在其 y 轴上旋转这是怎么发生的,我也试图生成地面 class 但我不认为我这样做了。 并查看了 ursina 的所有文档并查看了如何制作视频,我仍然不明白为什么。 我做错了什么?

from ursina import *

from ursina.prefabs.first_person_controller import FirstPersonController



app = Ursina()

EditorCamera()
cam = FirstPersonController()


class Player(Entity):
    def __init__(self, **kwargs):
        super().__init__(model='cube', color=color.orange, collider='box', origin = (0, 0, -2), parent = cam)
        self.health_bar = Entity(parent=self, model='quad', color=color.red, scale_y=.1, y=1.2)
        self.max_health = 100
        self.health = self.max_health








      


class Beings(Entity):
    def __init__(self, **kwargs):
        super().__init__(model='cube', color=color.gray, scale=2, collider='box', position=Vec3(1,0,2), origin_y=-.5)
        self.health_bar = Entity(parent=self, model='quad', color=color.red, scale_y=.1, y=1.2)
        self.max_health = 100
        self.health = self.max_health

       

class Ground(Entity):
    def __init__(self, **kwargs):
        super().__init__(model='plane', color=color.gray, scale=2, collider='mesh', position=Vec3(1,0,2), origin_y=-.4)
        self.health_bar = Entity(parent=self, model='quad', color=color.red, scale_y=.1, y=1.2)
        self.max_health = 100
        self.health = self.max_health

class Buildings(Entity):
     def __init__(self, **kwargs):
        super().__init__(model='cube', color=color.gray, scale=2, collider='box', position=Vec3(1,0,2), origin_y=-.5)
        self.health_bar = Entity(parent=self, model='quad', color=color.red, scale_y=.1, y=1.2)
        self.max_health = 100
        self.health = self.max_health


class plants(Entity):
     def __init__(self, **kwargs):
        super().__init__(model='cube', color=color.gray, scale=2, collider='box', position=Vec3(1,0,2), origin_y=-.5)
        self.health_bar = Entity(parent=self, model='quad', color=color.red, scale_y=.1, y=1.2)
        self.max_health = 100
        self.health = self.max_health

class Earth(Entity):
     def __init__(self, **kwargs):
        super().__init__(model='cube', color=color.gray, scale=2, collider='box', position=Vec3(1,0,2), origin_y=-.5)
        self.health_bar = Entity(parent=self, model='quad', color=color.red, scale_y=.1, y=1.2)
        self.max_health = 100
        self.health = self.max_health


     






b = Beings()
p = Player()


def update():
          
    Ground()
    p.z += (held_keys['w'] - held_keys['s']) * time.dt * 6
    p.x += (held_keys['d'] - held_keys['a']) * time.dt * 6
   

    if p.intersects(b).hit:
             b.color = color.lime
             print('player is inside trigger box')
    else:
          b.color = color.gray

app.run()

它正在下落,因为地面高于玩家的底部。 这可以通过向下移动地面来解决,例如 y=-1。 此外,像您一样将 origin_y 设置为负值将移动 model 并向上碰撞。

我想这会对你有所帮助。 您必须更改地面和玩家的y坐标:

from ursina import *

from ursina.prefabs.first_person_controller import FirstPersonController



app = Ursina()

EditorCamera()
cam = FirstPersonController(y=2)


class Player(Entity):
    def __init__(self, **kwargs):
        super().__init__(model='cube', color=color.orange, collider='box', origin = (0, 0, -2), parent = cam)
        self.health_bar = Entity(parent=self, model='quad', color=color.red, scale_y=.1, y=1.2)
        self.max_health = 100
        self.health = self.max_health

    


class Beings(Entity):
    def __init__(self, **kwargs):
        super().__init__(model='cube', color=color.gray, scale=2, collider='box', position=Vec3(1,0,2), origin_y=-.5)
        self.health_bar = Entity(parent=self, model='quad', color=color.red, scale_y=.1, y=1.2)
        self.max_health = 100
        self.health = self.max_health

    

class Ground(Entity):
    def __init__(self, **kwargs):
        super().__init__(model='plane', texture_scale=(10, 10), texture='white_cube', scale=10, collider='mesh', position=Vec3(1,0,2))
        self.health_bar = Entity(parent=self, model='quad', color=color.red, scale_y=.1, y=1.2)
        self.max_health = 100
        self.health = self.max_health
        self.y = -1 #change this as per your requirment
        self.texture='white_cube'

class Buildings(Entity):
    def __init__(self, **kwargs):
        super().__init__(model='cube', color=color.gray, scale=2, collider='box', position=Vec3(1,0,2), origin_y=-.5)
        self.health_bar = Entity(parent=self, model='quad', color=color.red, scale_y=.1, y=1.2)
        self.max_health = 100
        self.health = self.max_health


class plants(Entity):
    def __init__(self, **kwargs):
        super().__init__(model='cube', color=color.gray, scale=2, collider='box', position=Vec3(1,0,2), origin_y=-.5)
        self.health_bar = Entity(parent=self, model='quad', color=color.red, scale_y=.1, y=1.2)
        self.max_health = 100
        self.health = self.max_health

class Earth(Entity):
    def __init__(self, **kwargs):
        super().__init__(model='cube', color=color.gray, scale=2, collider='box', position=Vec3(1,0,2), origin_y=-.5)
        self.health_bar = Entity(parent=self, model='quad', color=color.red, scale_y=.1, y=1.2)
        self.max_health = 100
        self.health = self.max_health


    






b = Beings()
p = Player()
g = Ground()

def update():
        
    
    p.z += (held_keys['w'] - held_keys['s']) * time.dt * 6
    p.x += (held_keys['d'] - held_keys['a']) * time.dt * 6


    if p.intersects(b).hit:
            b.color = color.lime
            print('player is inside trigger box')
    else:
        b.color = color.gray

app.run()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM