我刚开始做一个3d游戏的时候,我创造了一种方法来拿起某些实体的盒子,并移动他们周围的球员。当我下降他们,但他们仍然漂浮在他们原来的地方。我如何添加重力的实体?就像当我下降的盒子,盒子将下降与像动态物理像在统一。
下面是我的代码:
import ursina.application
from ursina import *
from ursina.prefabs.first_person_controller import FirstPersonController
app = Ursina()
window.fullscreen = True
window.fps_counter.enabled = False
window.color = color.black
window.exit_button.enabled = False
#===========================================
#from Player import _Player_
from Map import *
#==============================
player = FirstPersonController()
player.jump_up_duration = 1
player.jump_height = 5
player.gravity = 0.8
player.speed = 15
player.cursor.color = color.blue
player.y = 10
player.cursor.model = 'circle'
#===================movables
pbox = Entity(model = 'cube', scale = 1, position = (5, 1, 5), collider = 'box', name = 'box1')
pbox.add_script()
global pbox_hold
pbox_hold = False
#===============================
def exit_game():
app.userExit()
def show_game_menu():
Exit.enabled = True
game_menu_bd.enabled = True
def hide_game_menu():
Exit.enabled = False
game_menu_bd.enabled = False
def pause_handler_input(key):
if key == 'left mouse down' and mouse.hovered_entity == back_game:
back_game.enabled = False
hide_game_menu()
application.paused = not application.paused
mouse.locked = True
if key == 'left mouse down' and mouse.hovered_entity == Exit:
exit_game()
game_menu_bd = Button(scale = 10)
game_menu_bd.z = 0.1
game_menu_bd.enabled = False
back_game = Button(scale = (0.15, 0.07), position = (0, 0), ignore_paused = True, color = color.azure, text = 'Resume')
back_game.input = pause_handler_input
back_game.enabled = False
Exit = Button(model = 'quad' , scale = (0.05, 0.035), position = (window.exit_button.x-0.025, window.exit_button.y-0.015), color = color.red, ignore_paused = True, text = 'exit')
Exit.input = pause_handler_input
Exit.enabled = False
global holding_box_E
holding_box_E = pbox
#===================
def input(key):
global pbox_hold, holding_box_E
if key == "c":
mouse.locked = True
if key == "escape" or key == "x":
show_game_menu()
mouse.locked = False
back_game.enabled = True
ursina.application.paused = True
if key == 'left mouse down' and pbox_hold == False:
cast = raycast(player.position+Vec3(0, 1.9, 0), direction=(player.camera_pivot.forward), distance=4, traverse_target=pbox, debug=True)
#print(cast.hit)
if cast.hit:
pbox.color = color.blue
pbox_hold = True
holding_box_E = cast.entity
elif key == 'left mouse up' and pbox_hold == True:
pbox_hold = False
def update():
global pbox_hold, holding_box_E
if player.y < -20:
player.position = (0, 2, 0)
if pbox_hold == True:
holding_box_E.position = player.position + Vec3(0, 1.5, 0) + player.camera_pivot.forward + Vec3(player.camera_pivot.forward.x, player.camera_pivot.forward.y, player.camera_pivot.forward.z)*2
app.run()
请注意,我有一个不同的脚本Map!!!
1条答案
按热度按时间atmip9wb1#
我刚刚为ursina找到了一个很棒的库,可以将物理实现为实体。
Bullet-for-Ursina
这个包可以让你在场景/世界中实现重力,给物体带来动态的物理和弹性。我会推荐,因为它很容易放进你的游戏。