Hi everyone! I am currently attempting to improve my back-stab code by adding y-direction dependency. Right now, I am purely shooting a ray straight out of the LocalPlayer's HumanoidRootPart. I am curious as to how I could make it so the ray's direction also caters to the y-direction which the player is looking (the game forces first person, so I assume this would involve the camera to some degree).
I'm not exactly sure where to start in order to make this amendment, but all help and tips are appreciated!
Here is my current code:
function CheckHit()
local Ray = Ray.new(C.Character.HumanoidRootPart.Position, C.Character.HumanoidRootPart.CFrame.lookVector * 4)
local Part, Pos, Normal = workspace:FindPartOnRayWithIgnoreList(Ray, {C, workspace.CurrentCamera, C.Character["Right Arm"], C.Character["Left Arm"]})
if Part then
if Part.Name == ("Torso" or "Left Arm" or "Right Arm" or "Head" or "Right Leg" or "Left Leg") then
local V = Part.Parent
local Product = game.Workspace[V.Name].Torso.CFrame.lookVector:Dot(C.Character.HumanoidRootPart.CFrame.lookVector)
if Product >= .5 then
ChangeMouseIcon("EnemyHit")
return(Part)
else
end
end
end
end
This code will check to make sure that you are behind the player you are stabbing, and that you can reach them. I'm not sure how to manipulate the ray's CFrame in order to respect the CurrentCamera's y-direction.
A visual interpretation of the change I am attempting to make:
Current: https://imgur.com/a/VwpYz0l

3 comments
no need for RaySpread you can remove it if you want
Since you stated it LocksFirstPerson, read this source: https://devforum.roblox.com/t/center-of-screen-in-world-coordinates/101939/2. I had difficulty with making my ray shooting in the center of the screen for Mobile players so I used that method. If your game is not mobile friendly you won't need to use it
That's actually awesome! Didn't think about using hit. I'll get back to you on how it goes, and accept the solution once I'm sure I don't have any further questions. Thank you!