'How do I put gravity on a character in ursina engine?
(Python Ursina Engine)
How can I add gravity on ursina engine?? Here is the code that I'm using: https://github.com/pokepetter/ursina/blob/master/samples/terraria_clone.py
from ursina import *
from math import floor
app = Ursina()
size = 32
plane = Entity(model='quad', color=color.azure, origin=(-.5,-.5), z=10, collider='box', scale=size) # create an invisible plane for the mouse to collide with
grid = [[Entity(model='quad', position=(x,y), texture='white_cube', enabled=False) for y in range(size)] for x in range(size)] # make 2d array of entities
player = Entity(model='quad', color=color.orange, position=(16,16,-.1))
cursor = Entity(model=Quad(mode='line'), color=color.lime)
def update():
if mouse.hovered_entity == plane:
# round the cursor position
cursor.position = mouse.world_point
cursor.x = round(cursor.x, 0)
cursor.y = round(cursor.y, 0)
# check the grid if the player can move there
if held_keys['a'] and not grid[int(player.x)][int(player.y)].enabled:
player.x -= 5 * time.dt
if held_keys['d'] and not grid[int(player.x)+1][int(player.y)].enabled:
player.x += 5 * time.dt
def input(key):
if key == 'left mouse down':
grid[int(cursor.x)][int(cursor.y)].enabled = True
if key == 'right mouse down':
grid[int(cursor.x)][int(cursor.y)].enabled = False
camera.orthographic = True
camera.fov = 10
camera.position = (16,18)
# enable all tiles lower than 16 to make a ground
for column in grid:
for e in column:
if e.y <= 15:
e.enabled = True
app.run()
Solution 1:[1]
You can use the built-in 2D platformer controller:
from ursina.prefabs.platformer_controller_2d import PlatformerController2d
player = PlatformerController2d(position=(16, 20, -.1), scale_y=1)
def update():
# other keys...
if held_keys['space']:
player.y += 1
This gives you rudimentary jumping control but a better way to implement that would be to define the level as a mesh entity and set it as the player's traverse_target
, as described in the Platformer Tutorial.
Solution 2:[2]
If you are making a 3d game, just put:
from ursina.prefabs.first_person_controller import FirstPersonController
Player = FirstPersonController()
into your code.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Jan Wilamowski |
Solution 2 | spiceinajar |