I’m working on a ledge grab system similar to dying light and I wanted a way to check if the player was looking back to I could play an animation, like how the player points their arm where they’re looking if they look back in dying light.
However for some reason its constantly playing and stopping repeatedly.
I’m not sure if my solution was the best for checking when and if the player looks back so if there’s a better way please let me know.
RunService.RenderStepped:Connect(function()
local dot = Vector3.new(Camera.CFrame.LookVector.X,0,Camera.CFrame.LookVector.Z).Unit:Dot(Vector3.new(Character.Head.CFrame.LookVector.X,0,Character.Head.CFrame.LookVector.Z).Unit)
if dot > 0 then
if LookingBack == true then
LookingBack = false
print("LookingForward")
LedgeBack:Stop()
end
else
if LookingBack == false then
LookingBack = true
print("LookingBack")
LedgeBack:Play()
end
end
end)
So you’re doing RunService.RenderStepped so it runs every x (For the life of me can’t remember the number) so you’re setting your ‘dot’ every time that starts makes sense, checking if ‘dot’ is bigger than 0 again makes sense, now if your LookingBack is true, you set your LookingBack to false, back to the first part that i wrote it runs every x times, now the code runs again, your dot is set again, it is indeed bigger than 0 again, however this time your LookingBack is false, it’s set to true now, your code runs again LookingBack is true… I think you get the idea, so basically you’re checking a value against itself then rerunning the exact same code checking for the opposite which triggers your else statement
I’m not a 100% certain what would be best to do for this, something I’d consider would be firing a raycast from the players camera checking if a part is hit (Albeit this isn’t near a 100% guarantee that it doesn’t give some precision errors in it’s detectance) gonna write some concept code that won’t run but it’ll give you an idea.
LookingBack = false
RunService.RenderStepped:Connect(function()
local origin = Head.Position
local direction = CFrame.lookAt(origin, Mouse.p).lookVector * 4
local rayParams = RaycastParams.new()
local returnedHit = workspace:Raycast(origin, direction, rayParams)
if not returnedHit then
if LookingBack then
return
end
LookingBack = true
LedgeBack:Play()
elseif returnedHit and LookingBack then
LookingBack = false
LedgeBack:Stop()
end
end)
So the idea is when the code runs, if nothing is returned from the raycast then you’re looking back, in theory, but you don’t want to keep triggering it so simply return if you’ve already toggled LookingBack to true and of course, if something is returned, well then you’re not looking back, no reason to try toggling your value and stopping your animation it keeps detecting parts so your elseif would check for a returned part and if your LookingBack is still true