nHyper
nHyper

Reputation: 55

Raycast not hitting anything in python ursina

This program will have a cube, a ground underneath and a raycast to detect the ground.

enter image description here

Issue

But the raycast is not hitting anything, even if i change the "direction" section to anything

Code

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

I've tried some of the methods i found from the web, but non of them seems to be working fine for me. How can i fix this?

Upvotes: 1

Views: 470

Answers (1)

pokepetter
pokepetter

Reputation: 1489

You have to add a collider. Example: ground = Entity(model='cube', collider='box', scale=(15,1,15))

Upvotes: 2

Related Questions