[英]Entity Gravity ursina
當我創建一種方法來拾取某些實體的盒子並與玩家一起移動它們時,我開始制作 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()
請注意,我在不同的腳本上有地圖!!!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.