Hi everyone,
I'm currently encountering an issue with a Pick operation in my Lua code and I'm hoping someone can help me figure out what I'm missing.
Here's the context: I have a player controller, which is essentially a pivot that creates a light and a camera. The player controller doesn't involve any movement, the mouse position on the screen simply influences the camera rotation.
I also have an enemy that randomly spawns and passes in front of the light, down a corridor. I want to check two things - if the light is on, and if the enemy crosses the light's path. If both conditions are met, I want to call a method from the player script to the enemy script.
Here's the relevant part of my player controller code which handles the light being on and the Pick operation:
function Script:UpdateWorld()
[...]
if not self.flashlight:Hidden() then
local pickInfo = PickInfo()
local i**** = self.entity.world:Pick(
self.entity:GetPosition(),
self.lightRayEnd:GetPosition(),
pickInfo,
.25,
false,
Collision.Character
)
if i**** and pickInfo.entity:GetKeyValue("classname") == "npc_nurse" then
pickInfo.entity.script:SetPlayerIsShiningLight()
end
end
end
The flashlight check works as expected. However, I'm having trouble with the Pick method. It seems to correctly pick up the enemy when it crosses the ray's path. But it also appears to pick up another entity. I've simplified my map to include only a floor for the enemy to walk on and the basic set-up for the player controller. I've confirmed that the KeyValue is being correctly set in the enemy script (in the Start method), and that the collision is set to Character.
Through debugging (I've removed all the console debug statements from the code above for clarity), I found that when I test for the pick position, it returns 0,0,0.
To make sure I wasn't missing something in my calculations, I created another pivot to mark the end of the raycast (self.lightRayEnd).
Does anyone have any idea what I could be doing wrong, or what might be causing this issue? I appreciate any input or suggestions. Thanks!