-2

我正在尝试使用 ursina 制作游戏,但是在运行我的代码时出现了一些问题

我希望我的立方体在我按下arrows和相机移动时移动wasd

它工作得很好,但立方体在移动时一直在摇晃

当我按下UP arrow时,它会随着震动而移动

但是当我停止按下时它不会arrows

它只是不能流畅地移动

有什么办法可以解决这个问题吗?

我的代码一定有问题

这是我的代码:

import ursina as us

app = us.Ursina()


class Player(us.Entity):
    def __init__(self):
        super().__init__(
            parent=us.scene,
            model='cube',
            position=(0, 1, 0),
            rotation=(0, 45, 0),
            texture='white_cube',
            color=us.color.azure,
            )
        us.camera.position = (0, 2, 5)
        us.camera.look_at(self)
        
        self.speed = 5
        self.gravity = 0.01
        self.strength = 0.2
        self.v = None
        
    def update(self):
        us.camera.look_at(self)
        
        if us.held_keys['up arrow']:
            self.position += self.forward * us.time.dt * self.speed
        if us.held_keys['down arrow']:
            self.position += self.back * us.time.dt * self.speed
        if us.held_keys['right arrow']:
            self.rotation += (0, 5, 0)
        if us.held_keys['left arrow']:
            self.rotation += (0, -5, 0)
        
        if us.held_keys['w']:
            us.camera.position += us.camera.forward * us.time.dt
        if us.held_keys['s']:
            us.camera.position += us.camera.back * us.time.dt
        if us.held_keys['d']:
            us.camera.position += us.camera.right * us.time.dt
        if us.held_keys['a']:
            us.camera.position += us.camera.left * us.time.dt
        
        if us.held_keys['left shift']:
            player.speed = 10
        else:
            player.speed = 5
        
        if us.held_keys['space']:
            self.jump()
        
        
        if self.v != None:
            self.v -= self.gravity
            if self.v > -self.strength:
                self.position += (0, self.v, 0)
            else:
                self.v = None
        
        
    def jump(self):
        if self.v == None:
            self.v = self.strength

if __name__ == '__main__':
    
    player = Player()
    us.Sky()
    
    
    app.run()

任何帮助,将不胜感激

谢谢

4

1 回答 1

0

移动camera.look_at(self)到更新函数的底部,因此它会在您移动相机后执行look_at。

于 2021-07-23T22:12:56.190 回答