Raycast 在 python ursina 中没有击中任何东西
Raycast not hitting anything in python ursina
此程序将有一个立方体、下方的地面和用于检测地面的光线投射。
问题
但是光线投射没有击中任何东西,即使我将“方向”部分更改为任何东西
代码
from ursina import *
from random import randint
app = Ursina()
#default entities
player = Entity(model='cube', color=color.orange, texture='white_cube')
main_ground = Entity(model='cube', color=color.gray, texture='white_cube', scale=(5,15,1))
ground_segment_1 = Entity(model='cube', color=color.gray, texture='white_cube', scale=(5,15,1))
ground_segment_2 = Entity(model='cube', color=color.gray, texture='white_cube', scale=(5,15,1))
ground_segment_3 = Entity(model='cube', color=color.gray, texture='white_cube', scale=(5,15,1))
ground_segment_4 = Entity(model='cube', color=color.gray, texture='white_cube', scale=(5,15,1))
ground_segment_5 = Entity(model='cube', color=color.gray, texture='white_cube', scale=(5,15,1))
#entities default pos
main_ground.y = - (main_ground.scale_y / 2 + 0.5)
player.y = main_ground.y + (main_ground.scale_y / 2 + 0.5)
player.x = main_ground.x - 4
#default var
random_floor_spawn = randint(main_ground.scale_x, main_ground.scale_x * 9) / 10
vel_x = 0.1
vel_y = 0
#game launch
def update():
#### GROUND ####
#follow player
main_ground.x = round((player.x + 4) / main_ground.scale_x) * main_ground.scale_x - main_ground.scale_x * 2.5 + random_floor_spawn
#segments
ground_segment_1.x= main_ground.x + main_ground.scale_x * 1
ground_segment_1.y= main_ground.y
ground_segment_2.x= main_ground.x + main_ground.scale_x * 2
ground_segment_2.y= main_ground.y
ground_segment_3.x= main_ground.x + main_ground.scale_x * 3
ground_segment_3.y= main_ground.y
ground_segment_4.x= main_ground.x + main_ground.scale_x * 4
ground_segment_4.y= main_ground.y
ground_segment_5.x= main_ground.x + main_ground.scale_x * 5
ground_segment_5.y= main_ground.y
#### PLAYER ####
#movement
player.x += vel_x
player.y += vel_y
#collide check
hit_info = raycast(origin= player.position, direction= (0, -1, 0), ignore= (player), distance= inf, debug= False)
#### CAMERA ####
#cam
camera.x = player.x + 4
camera.y = player.y + 2
#debug
print(hit_info.hit)
app.run()
我已经尝试了一些从网上找到的方法,但似乎没有一种适合我。
我该如何解决这个问题?
你必须添加一个碰撞器。例子:
ground = Entity(model='cube', collider='box', scale=(15,1,15))
此程序将有一个立方体、下方的地面和用于检测地面的光线投射。
问题
但是光线投射没有击中任何东西,即使我将“方向”部分更改为任何东西
代码
from ursina import *
from random import randint
app = Ursina()
#default entities
player = Entity(model='cube', color=color.orange, texture='white_cube')
main_ground = Entity(model='cube', color=color.gray, texture='white_cube', scale=(5,15,1))
ground_segment_1 = Entity(model='cube', color=color.gray, texture='white_cube', scale=(5,15,1))
ground_segment_2 = Entity(model='cube', color=color.gray, texture='white_cube', scale=(5,15,1))
ground_segment_3 = Entity(model='cube', color=color.gray, texture='white_cube', scale=(5,15,1))
ground_segment_4 = Entity(model='cube', color=color.gray, texture='white_cube', scale=(5,15,1))
ground_segment_5 = Entity(model='cube', color=color.gray, texture='white_cube', scale=(5,15,1))
#entities default pos
main_ground.y = - (main_ground.scale_y / 2 + 0.5)
player.y = main_ground.y + (main_ground.scale_y / 2 + 0.5)
player.x = main_ground.x - 4
#default var
random_floor_spawn = randint(main_ground.scale_x, main_ground.scale_x * 9) / 10
vel_x = 0.1
vel_y = 0
#game launch
def update():
#### GROUND ####
#follow player
main_ground.x = round((player.x + 4) / main_ground.scale_x) * main_ground.scale_x - main_ground.scale_x * 2.5 + random_floor_spawn
#segments
ground_segment_1.x= main_ground.x + main_ground.scale_x * 1
ground_segment_1.y= main_ground.y
ground_segment_2.x= main_ground.x + main_ground.scale_x * 2
ground_segment_2.y= main_ground.y
ground_segment_3.x= main_ground.x + main_ground.scale_x * 3
ground_segment_3.y= main_ground.y
ground_segment_4.x= main_ground.x + main_ground.scale_x * 4
ground_segment_4.y= main_ground.y
ground_segment_5.x= main_ground.x + main_ground.scale_x * 5
ground_segment_5.y= main_ground.y
#### PLAYER ####
#movement
player.x += vel_x
player.y += vel_y
#collide check
hit_info = raycast(origin= player.position, direction= (0, -1, 0), ignore= (player), distance= inf, debug= False)
#### CAMERA ####
#cam
camera.x = player.x + 4
camera.y = player.y + 2
#debug
print(hit_info.hit)
app.run()
我已经尝试了一些从网上找到的方法,但似乎没有一种适合我。 我该如何解决这个问题?
你必须添加一个碰撞器。例子:
ground = Entity(model='cube', collider='box', scale=(15,1,15))