Make raycast direction go along a wall that it hits

Basically what I’m trying to do from a top down perspective can be illustrated with this


once the ray hits the wall, its direction, or maybe just the component of the ray that goes along the surface is kept. either solution will do, I think the latter makes more sense. In other words, I need to find the projection of the ray onto the wall

You can just cast raycast and if it hitted wall it will start new raycast in wall direction:
example:

local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Exclude
params.FilterDescendantsInstances = {workspace.cast}
local ray = workspace:Raycast(workspace.cast.Position,workspace.cast.CFrame.LookVector*100,params)
if ray then
	if ray.Instance then
		local params2 = RaycastParams.new()
		params2.FilterType = Enum.RaycastFilterType.Exclude
		local a = Instance.new('Part')
		params2.FilterDescendantsInstances = {ray.Instance,a}
		a.Position = ray.Instance.Position
		a.Rotation = ray.Instance.Rotation
		local ray2 = workspace:Raycast(ray.Position,a.CFrame.XVector*100,params2) -- you can change XVector(right) to -XVector(left)
		if ray2 then
			if ray2.Instance then
				print(ray2.Instance.Name)
			end
		end
		a:Destroy()
	end
end

you can modify the script as you want

You can simply calculate projection of the ray onto wall just by checking ray angle that it casted,and change XVector.

I just did this my actual problem was to do with movement direction to prevent janky collisions with walls when the player walks into them
this works

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