Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How can I add vertical direction to my current backstab code?

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

1 answer

✓ Accepted answer

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)

3 comments

0

no need for RaySpread you can remove it if you want

0

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

0

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!

This discussion is preserved as it was. New answers, comments, and votes are closed.