How to shoot a raycast out from behind the player?

I am trying to detect when the players back is up against a wall, I was figuring raycasts would be the easiest way to do this and have it setup now shooting out of the torso but it is shooting the raycast forward instead of out the back. I was wondering if this was the best method to be doing this and if so how I can make it shoot out the back.

negate the LookVector of the player’s torso so that it is facing the other way

I attempted to do that but couldn’t figure out how to, do you know how?

Here is the code I currently have.

mouse.KeyDown:Connect(function(key)
	if key == "q" then
		local lv = character.Torso.CFrame.LookVector
		local origin = character.Torso.Position
		local direction = lv * 100
		local raycastResult = workspace:Raycast(origin,direction)
		if raycastResult then
			print(player:DistanceFromCharacter(raycastResult.Instance.Position))
		end
	end
end)

All you need to do is turn that 100 into -100

1 Like
mouse.KeyDown:Connect(function(key)
	if key == "q" then
		local lv = (character.Torso.CFrame.LookVector * -1)
		local origin = character.Torso.Position
		local direction = lv * 100
		local raycastResult = workspace:Raycast(origin,direction)
		if raycastResult then
			print(player:DistanceFromCharacter(raycastResult.Instance.Position))
		end
	end
end)

dunno, i’m not that great at scripting.

It prints out this?
image

all the same numbers

i guess you’d be ~1.7 studs from the result?

I would to but I am not 1.7 studs from there

my apologies if i’m not helpful.

1 Like

The way I debug something like this is to make a part at either the RayCast position or offset a part from the torso position by the LookVector

I was thinking that it could be hitting the item my avatar has on his back but not sure

Putting a part at RayCastResult.Position would be pretty helpful for this. Also, you can use RaycastResult.Distance to get the distance of the raycast instead of calculating magnitude

The reason the script doesn’t work is probably because you didn’t filter the character so it could possibly see it as a part behind the character, when it’s just a part in the character. I made the following script and it works.

local UserInputService = game:GetService("UserInputService")
local Player = game.Players.LocalPlayer

function Raycast(Character)
	local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
	local Direction = HumanoidRootPart.CFrame.LookVector * -1
	local RaycastResult = workspace:Raycast(HumanoidRootPart.Position, Direction * 1.5)
	local RaycastParameters = RaycastParams.new()
	RaycastParameters.FilterType = Enum.RaycastFilterType.Blacklist
	RaycastParameters.FilterDescendantsInstances = {Character}
	if RaycastResult then
		print(RaycastResult.Position)
	end
end

UserInputService.InputBegan:Connect(function(Key, Focused)
	if Focused then return end
	if Key.KeyCode == Enum.KeyCode.Q then
		Raycast(Player.Character)
	end
end)

okay that makes a lot more sense thank you