1

I'm trying to make a horror type game in Ursina as a small project but I can't figure out how to position the point light. I want the point light to be pointing forward a bit and be at the players position. I practically just want to make a flashlight for my character.

This is the full code at the moment:

from ursina import *
from random import uniform
from ursina.prefabs.first_person_controller import FirstPersonController
from ursina.shaders import lit_with_shadows_shader
app = Ursina()
player = FirstPersonController(model='cube', color=color.orange, origin_y=-.5, speed=8, position = (-52, 0, 57))
player.collider = BoxCollider(player, Vec3(0,1,0))
player.visible = False
ammo = 10
playerHealth = 100
editor_camera = EditorCamera(enabled=False, ignore_paused=True)           
ammoCounter = Text(text= ammo + ammo, position = (-0.4,-0.4), color = color.black)
ttt=PointLight(position= (-52, 0, 57), parent=camera.ui, color = color.rgba(100, 2, 100, 0), rotation = (0, 184, 0), shadows = True)
    
def update(): ##ammo counter ##
    ammoCounter.text = ammo 

def pause_input(key): ##editor camera thing##
    if key == 'tab':    # press tab to toggle edit/play mode
        editor_camera.position = player.position 
        editor_camera.rotation = (90,180,0)
        editor_camera.y = 50
        editor_camera.enabled = not editor_camera.enabled
        player.visible =True
        player.visible_self = editor_camera.enabled
        player.cursor.enabled = not editor_camera.enabled
        gun.enabled = not editor_camera.enabled
        mouse.locked = not editor_camera.enabled
        application.paused = editor_camera.enabled
pause_handler = Entity(ignore_paused=True, input=pause_input)
    

def input(key): ## gunshot, gun, player shift, and ammo crates destroys
    global ammo 
    
    if key == "2":
        ttt.color = color.black

    if key == "1":
        ttt.color = color.white

    
    ## destroy ammo crates##
    if key == 'e':
        if mouse.hovered_entity == ammo1: 
            if distance(player, ammo1) <= 5:
                try: 
                    ammo += 10
                except:
                    print('ammo issue')
                try:
                    destroy(ammo1)
                except:
                    print("ammo box issue")
        if mouse.hovered_entity == ammo2: 
            if distance(player, ammo2) <= 5:
                try: 
                    ammo += 10
                except:
                    print('ammo issue')
                try:
                    destroy(ammo2)
                except:
                    print("ammo box issue")
        if mouse.hovered_entity == ammo3: 
            if distance(player, ammo3) <= 5:
                try: 
                    ammo += 10
                except:
                    print('ammo issue')
                try:
                    destroy(ammo3)
                except:
                    print("ammo box issue")
        if mouse.hovered_entity == ammo4: 
            if distance(player, ammo4) <= 5:
                try: 
                    ammo += 10
                except:
                    print('ammo issue')
                try:
                    destroy(ammo4)
                except:
                    print("ammo box issue")
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   

   
    if key == 'k':
        print (player.position)
        print (player.rotation)
        print (ammo)
        print (ttt.position)

    if key=="left mouse down" and ammo > 0:
        ammo -= 1
        if mouse.hovered_entity and hasattr(mouse.hovered_entity, 'hp'):
            mouse.hovered_entity.hp -= 10
            mouse.hovered_entity.blink(color.black)
            print (ammo)
        Audio("gunshot")
        if gun.position == (0.45,-0.2,0):
            Animation("spark", parent=camera.ui, fps=5, scale=.15, position=(0.28, -0.01), loop=False)
            print (ammo)
        if gun.position == (0,-0.15,0):
            Animation("spark", parent=camera.ui, fps=5, scale=.15, position=(-0.005,0.08,0), loop=False)
            print (ammo)
        
    if held_keys["q"]:
            gun.position = (0,-0.15,0)
            gun.rotation = (0,0,0)
            Cursor = False
            player.speed = 1
            if held_keys["shift"]:
                player.speed = 100
            
    else: 
        gun.position = (0.45,-0.2,0)
        gun.rotation = (5,30,0)
        player.speed = 250 * time.dt
    if held_keys["shift"]:
        player.speed = 400 * time.dt
        



class AmmoR(Entity): ## ammo refill class#
    def __init__(self, **kwargs):
        super().__init__(model='cube', texture = 'ammobox', scale = 1, origin_y=-0.5, collider = "mesh", **kwargs)

## amount of amo refill boxes##

ammo1 = AmmoR() 
ammo2 = AmmoR()
ammo3 = AmmoR()
ammo4 = AmmoR()
ammo1.position = (-9,2,53)
ammo2.position = (50,1,21)
ammo3.position = (-17,0,31)
ammo4.position = (15,0,8)


        
        



class Enemy(Entity): ##enemy class#
    def __init__(self, **kwargs):
        super().__init__(model='cube', scale_y=4, origin_y=-0.5, color=color.light_gray, collider='box',  texture = "monster", **kwargs)
        self.health_bar = Entity(parent=self, y=1.2, model='cube', color=color.red, world_scale=(1.5,.1,.1))
        self.max_hp = 100
        self.hp = self.max_hp

    def update(self):
        global playerHealth
        dist = distance_xz(player.position, self.position)
        if dist > 40:
            self.health_bar.visible = False
            return

        self.health_bar.alpha = max(0, self.health_bar.alpha - time.dt)


        self.look_at_2d(player.position, 'y')
        hit_info = raycast(self.world_position + Vec3(0,1,0), self.forward, 30, ignore=(self,))
        if hit_info.entity == player:
            if dist > 1:
                self.position += self.forward * time.dt * 5

        if player.intersects(self).hit:
            print(playerHealth)
            playerHealth -= 0.3


    @property
    def hp(self):
        return self._hp

    @hp.setter
    def hp(self, value):
        self._hp = value
        if value <= 0:
            destroy(self)
            return

        self.health_bar.world_scale_x = self.hp / self.max_hp * 1.5
        self.health_bar.alpha = 1

## ENEMYS FIRST LEVEL
enemie1 = Enemy()
enemie2 = Enemy()
enemie3 = Enemy()
enemie4 = Enemy()
enemie5 = Enemy()
enemie6 = Enemy()
enemie7 = Enemy()
enemie8 = Enemy()
enemie9 = Enemy()
enemie10 = Enemy()
enemie11 = Enemy()

enemie1.position = (10,0,-2)
enemie2.position = (51.9096, 0, -57.6378)
enemie3.position = (53.7879, 0, -57.4827)
enemie4.position = (53.5366, 0, -39.0536)
enemie5.position = (50,0,-2)
enemie6.position = (50,0,-5)
enemie7.position = (22,0,-20)
enemie8.position = (55,0,-4)
enemie9.position = (15,0,-2)

Sky(color = color.black)


maze=Entity(model="maze", scale=10, texture="BK", color= color.gold ,collider="mesh")


gun=Entity(model="colt", parent=camera.ui, scale=0.07, texture='Maybe', position=(0.45,-0.2,0), rotation=(5,30,0))





app.run()

I just need to figure out on:

ttt=PointLight(position= (-52, 0, 57), parent=camera.ui, color = color.rgba(100, 2, 100, 0), rotation = (0, 184, 0), shadows = True)

and see if I can add a def update() function to make it go to the player position. The only thing is that when I change the position of the light I see no change in the effect.

I tried to move the light to another position but it stayed the same. I am also trying to figure out how to change the brightness of the light.

Onewaypie
  • 31
  • 4
  • 1
    It seems like you have to set the parent of the torch to the character's body e.g., to the hand, instead of camera.ui to make it work with the position. Is your gun working properly? – Jishan Shaikh Dec 07 '22 at 12:47
  • Yes, the gun works properly but it does not use raycasting, it just uses if the mouse if hovering over the entity since I don't know how to use raycasting. I will try that out right now and see if it works. – Onewaypie Dec 07 '22 at 13:58
  • I just added it to parent the player but It acts a bit weird as it shines light to the side of the player. Is there any way to make it follow to player rotation and act like a flashlight and also have a limited distance? @JishanShaikh – Onewaypie Dec 07 '22 at 14:03
  • 1
    Try changing the rotation parameter of the torch to your liking. Otherwise, set `look_at` of the torch as `torch.look_at(otherEntity)`, it also has an optional axis parameter as `torch.look_at(otherEntity, axis='up')`. Optionally enable `debug=True` for the torch element to see the axis separately for the torch entity. – Jishan Shaikh Dec 07 '22 at 14:28
  • I changed the code a bit but now if I do torch.look_at(player) it goes dark when i go near something. Is there any way to just make the torch follow the mouse point? For it to be at the player but to actually aim at the mouse and only be a certain size wide and only go a certain distance? @JishanShaikh – Onewaypie Dec 07 '22 at 16:18
  • 1
    For pointing light, you can use the PointLight [entity](https://www.ursinaengine.org/api_reference.html#PointLight). Set parent of it to be either player or `mouse.position`. If it doesn't suit your use case, you can also try AmbientLight and SpotLight. – Jishan Shaikh Dec 07 '22 at 16:27
  • @JishanShaikh I changed the code up a bit but now it kinda works. The issue is that the objects get darker as i get closer to them, not lighter. `ttt=PointLight(parent=mouse.position, color = color.rgba(100, 100, 100, 0.1), shadows = True) def update(): ttt.position = player.position ttt.rotation = player.rotation ` This is the code right now that I have for it. The rest of the main code for the game itself and for player are in the question, this is just the updated version of what I have added/changed – Onewaypie Dec 07 '22 at 16:44
  • 1
    Good job. Would love to see the complete work, maybe onto GitHub. – Jishan Shaikh Dec 07 '22 at 17:35
  • Thanks, but do you have any idea how I could use the light as a flashlight instead of it just being there? It's not pointing where i want it to even though the parent is the camera.ui. @JishanShaikh – Onewaypie Dec 07 '22 at 18:32
  • 1
    For movement of torch, you can do something like this with mouse: ```def update(self): self.rotation_y += mouse.velocity[0] * self.mouse_sensitivity[1] self.camera_pivot.rotation_x -= mouse.velocity[1] * self.mouse_sensitivity[0] self.camera_pivot.rotation_x= clamp(self.camera_pivot.rotation_x, -90, 90)``` – Jishan Shaikh Dec 07 '22 at 18:55
  • For more ideas on how to do something in Ursina, check out official prebuilt [samples](https://github.com/pokepetter/ursina/tree/master/samples). – Jishan Shaikh Dec 07 '22 at 18:57
  • 1
    I've been trying the code you gave me, it does not change anything I'm afraid. I've also looked at the Ursina documentation but it seems fairly light and there is not much documentation on it. I will try to work on it further but thanks for all your help. – Onewaypie Dec 07 '22 at 19:18

0 Answers0