Game freezes when making a SpotLight3D visible

Godot Version

4.2.1 (Steam)

Question

I have a SpotLight3D that acts as a flashlight. it uses a GradientTexture2D as the projector texture, and it points to wherever the mouse is facing using this code (the camera is stationary):

extends SpotLight3D

@onready var SpaceState = self.get_world_3d().direct_space_state

@onready var RayOrigin = $".".position
@onready var RayEnd = Vector3()

func _input(event):
	if (event is InputEventMouseMotion and $"../Status/OfficeState".get("flashlight") == 1) or (event is InputEventKey and Input.is_action_just_pressed("Flashlight")):
		var mouse_offset = get_viewport().get_mouse_position()
		
		RayEnd = RayOrigin + $"../SpotLight3D/Camera3D".project_ray_normal(mouse_offset) * 10000
		var params = PhysicsRayQueryParameters3D.new()
		
		params.to = RayOrigin
		params.from = RayEnd
		params.collide_with_areas = true
		params.hit_back_faces = true
		#params.collision_mask = 1
		var intersection = SpaceState.intersect_ray(params)
		
		if not intersection.is_empty():
			var pos = intersection["position"]
			$".".look_at(pos)

For some reason whenever I make the “visible” parameter true for the first time after running the game, it freezes for about 5 seconds, and lags a bit more after.

It works perfectly fine every subsequent time the visible flag is checked, just not the first.

please help, this i really doing my head in :sweat_smile:

That may just be because it starts computing the shadowmaps. I don’t think there’s anything wrong with your code. Try just placing another spotlight in the scene and see if that changes anything.
I think if you just add an Environment object to the scene it may help this particular problem.

1 Like

I already have a WorldEnvironment in the scene, am I supposed to configure it a certain way? Also when I added a second spotlight it just projected a light, the problem still persisted as well

Ok, then I am out of ideas, sorry. uwu

oh, rip

I have figured out the issue, instead of changing the visible variable I instead changed the energy value, no more freezing!

That makes a lot of sense and im gonna take note of that for the future.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.