Issue with FirstPersonController in Ursina

Question:

I’m using the Ursina engine to create a 3D game. However, when I try to load the FirstPersonCharacter, all I got is a grey background (normal) and a very small magenta square, in the center, tilted at 45°. What is that ?

I was first trying to make my own mechanics for a first person character, move the camera according to the mouse position (I have this), and I was playing with math and stuff for movements… I was looking at this video (https://www.youtube.com/watch?v=DHSRaVeQxIk) for totally something else, and I found out about the FirstPersonController.

But, with the (almost) same code as him, it doesn’t work ! What’s that issue, has someone already ran into it ? Is FirstPersonController broken ? Or is my brain broken ?

Edit : found out in the ursina cheatsheet that the small magenta tilted square is the cursor. But I still can’t move, can’t have gravity or anything ? And I can’t see my floor.

Second edit : using some piece of code the ursina cheatsheet provided, arranged, I can now see my floor. But I can only move the camera on 1 axis (up and down), I can’t move, no gravity, nothing…

Here is my code :

from ursina import *
from ursina.prefabs.first_person_controller import FirstPersonController

app = Ursina()

window.title = 'The lab'
window.borderless = False
window.fullscreen = True
window.exit_button.visible = False
window.fps_counter.enabled = True

floorcubes = []
for i in range(-20, 20, 2):
    for j in range(-20, 20, 2):
        floorcubes.append(Entity(model='cube', color=color.white, scale=(2,2,2), position = (i, 0, j)))
player = FirstPersonController()
app.run()

Here is the code provided in the ursina cheatsheet, arranged a bit :

from ursina import *
from ursina.prefabs.first_person_controller import FirstPersonController
app = Ursina()
ground = Entity(model='plane', scale=(100,1,100), color=color.yellow.tint(-.2), texture='white_cube', texture_scale=(100,100), collider='box', position = (0, -2, 0), grounded = True)
e = Entity(model='cube', scale=(1,5,10), x=2, y=.01, rotation_y=45, collider='box', texture='white_cube')
e.texture_scale = (e.scale_z, e.scale_y)
e = Entity(model='cube', scale=(1,5,10), x=-2, y=.01, collider='box', texture='white_cube')
e.texture_scale = (e.scale_z, e.scale_y)

player = FirstPersonController(model='cube', y=2, origin_y=-.5, gravity = 1)
player.gun = None

gun = Button(parent=scene, model='cube', color=color.blue, origin_y=-.5, position=(3,0,3), collider='box')
gun.on_click = Sequence(Func(setattr, gun, 'parent', camera), Func(setattr, player, 'gun', gun))

gun_2 = duplicate(gun, z=7, x=8)
slope = Entity(model='cube', collider='box', position=(0,0,8), scale=6, rotation=(45,0,0), texture='brick', texture_scale=(8,8))
slope = Entity(model='cube', collider='box', position=(5,0,10), scale=6, rotation=(80,0,0), texture='brick', texture_scale=(8,8))

def input(key):
    if key == 'left mouse down' and player.gun:
        gun.blink(color.orange)
        bullet = Entity(parent=gun, model='cube', scale=.1, color=color.black)
        bullet.world_parent = scene
        bullet.animate_position(bullet.position+(bullet.forward*50), curve=curve.linear, duration=1)
        destroy(bullet, delay=1)
app.run()

Thanks to the answer, I have now :

from ursina import *
from ursina.prefabs.first_person_controller import FirstPersonController
import pyautogui
import random
import math

app = Ursina()

is_fullscreen = True
window.title = 'The lab'
window.borderless = False
window.fullscreen = is_fullscreen
window.exit_button.visible = False
window.fps_counter.enabled = True

latest_mouse_pos = pyautogui.position()
pyautogui.FAILSAFE = False
sensibility = 2.5
mouse.visible = True


floorcube = Entity(model="cube", color = color.white, scale=(20, 1, 20), collider="box", position=(0, -100, 0))


def update():
    global latest_mouse_pos
    if held_keys['f']:
        camera.fov += 1
    if held_keys['r']:
        camera.fov -= 1

player = FirstPersonController()

app.run()
Asked By: Quantum Sushi

||

Answers:

There is gravity and it has the effect of letting the player fall into infinity. When you move the mouse around to look up, you will see the cubes disappearing into the distance.

The solution is to add collider='box' to your floor cubes to stop the player from falling through. Note that the starting point seems to be inside one of the cubes so you have to jump out of it (using the space bar) or slightly lower your floor cubes’ position.

Answered By: Jan Wilamowski
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.