1

I was starting to make a 3d game when i created a way to pick up certain entity's as boxes and move them around with the player. When i drop them however, they stay floating where they were. How to i add gravity to the entity? like when i drop the box, the box will fall with like with dynamic physics like in unity.

Here's my Code:

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()

NOTE that I have the map on a different script!!!

1 Answers1

1

I just found a great library for ursina for implementing physics into entities.

Bullet-for-Ursina

the package lets you implement gravity into the scene/world to give objects dynamic physics with bounciness as well. I would recommend since its easy to put in with you game.

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 25 '22 at 14:39
  • the solution meets the question, however you must include code and quotes. – Lixt Dec 27 '22 at 13:14