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
Goal: https://imgur.com/a/2joQ9ro
You can use Mouse.hit.p
and place it inside CFrame.new()
.
local Mouse = game.Players.LocalPlayer:GetMouse() local HRP = C.Character.HumanoidRootPart local RayRange = 4 local RaySpread = CFrame.Angles(0, 0, 0) local Ray = Ray.new(HRP.Position, (CFrame.new(HRP.Position, Mouse.Hit.p) * RaySpread).lookVector.unit * RayRange)