How can I make it so that a raycast can shoot directly forward?

I’m making a wall hopping system in my game, and I’m not that experienced with ray cast. I’m trying to figure out how to shoot a ray directly forward from the humanoids root part, should I use CFrame.LookVector?

local char = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()
local humanoid = char:WaitForChild("Humanoid")
local root = char.PrimaryPart

local origion = root.Position
local direction = origion.Unit * Vector3.new(2, 2, 5)

local part = Instance.new("Part", workspace)
part.Anchored = true
part.Position = origion
part.Size = direction
part.CanCollide = false

while task.wait() do
	origion = root.Position
	
	part.Position = origion
	part.Size = direction
	local forwardRay = workspace:Raycast(origion, direction)
	
	part.Position = origion
	part.Size = direction
	
	if forwardRay then
		print(forwardRay.Instance)
	end
end

I suggest instead of while task.wait() do, you use RunService, it runs every frame.
Plus HumanoidRootPart.CFrame.LookVector would work in fact, you’re correct there!

1 Like

Ok so, I’m having issues with the raycast now…

robloxapp-20230116-1530182.wmv (1.4 MB)

local char = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()
local humanoid = char:WaitForChild("Humanoid")
local root = char.PrimaryPart

local origion = root.CFrame.Position
local direction = root.CFrame.LookVector.Unit * Vector3.new(8, 1, 8)

local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Blacklist
params.IgnoreWater = true
params.FilterDescendantsInstances = {char}

local part = Instance.new("Part", workspace)
part.Anchored = true
part.Position = origion
part.Size = direction
part.CanCollide = false
part.Name = "Visualizer"

game:GetService("RunService").RenderStepped:Connect(function()
	origion = root.Position
	direction = root.CFrame.LookVector.Unit * Vector3.new(8, 1, 8)
	
	part.Position = origion
	part.Size = direction
	local forwardRay = workspace:Raycast(origion, direction, params)
	
	part.Position = origion
	part.Size = direction
	
	if forwardRay then
		print(forwardRay.Instance)
	end
end)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.