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

how to detect if the touched part is in front of the humanoid?

Sorry for any English mistakes, not native.

I have two problems in my double jump script.

The first one is that when the player tries to jump against a wall the humanoid is supposed to turn 90° to trigger one of the wall jump "sentences",the problem is that the Upper torso and head have hitboxes that sometimes don't colide with the wall.So I tried to use their positions using CFrames,but i'm terrible at CFrames and don't know how to do it.

The second problem is that when the walljump get trigged the player can spam it forever in the air because the touched event humanoid part still the same even after the touch ended,i tried replacing the hl name after the event occured but it doesn't seen to work.

local acceleration = require(workspace.acelleration)
local UIS = game:GetService("UserInputService")
local localPlayer = game.Players.LocalPlayer
local character = game.Players.LocalPlayer.Character
local humanoid = script.Parent:WaitForChild("Humanoid") 
local root = character:WaitForChild("HumanoidRootPart")
function walljumpright()
	humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
			root.Velocity = (root.CFrame.RightVector*-1+root.CFrame.LookVector*0.2)*(40+acceleration*2)
	
end
function walljumpleft()
	humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
			root.Velocity = (root.CFrame.RightVector+root.CFrame.LookVector*0.2)*(40+acceleration*2)
end
humanoid.Touched:connect(function(tp, hl)
	if hl.Name == "LeftUpperArm" and humanoid:GetState() == Enum.HumanoidStateType.Freefall then
		game.StarterPlayer.StarterCharacterScripts.doublejump.Disabled = true
		print("leftsurface")
			UIS.JumpRequest:Connect(walljumpleft)
		end
			if hl.Name == "RightUpperArm" and humanoid:GetState() == Enum.HumanoidStateType.Freefall then
				game.StarterPlayer.StarterCharacterScripts.doublejump.Disabled = true
		print("Rightsurface")
		UIS.JumpRequest:Connect(walljumpright)
			end
			if hl.Name == "UpperTorso" and humanoid:GetState() == Enum.HumanoidStateType.Freefall or
			 hl.Name == "Head" and humanoid:GetState() == Enum.HumanoidStateType.Freefall then
		print("middlesurface")
		root.CFrame = CFrame.Angles(0,math.rad(90),0)+ root.CFrame.p
			end
				game.StarterPlayer.StarterCharacterScripts.doublejump.Disabled = false
			hl = "placeholder"
				end)


here's the walljump script. The second problem I see some ways to fix , so the first problem is the most important one.

1 answer

I would send out a small raycast from the HumanoidRootPart. It will raycast where it is facing by using LookVector. If it returns a part, that means something is in front of it.

local hrp = Character.HumanoidRootPart

local r = Ray.new(hrp.Position, hrp.CFrame.lookVector * 5)
local hit, pos = workspace:FindPartOnRay(r, Character)
if (hit) then
	--something is in front
else
	--something isn't in front
end

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