Raycasting in the wrong direction

What do you want to achieve?

To raycast in the given direction to detect the touched material.

What is the issue?

I’ve done a lot of raycasting in the past with no issue but for some reason when getting UpVector it appears to use LookVector, LookVector as RightVector etc and I can’t figure out why.

What solutions have you tried so far?

I’ve asked some friends as well as google but found no solution as of yet. I’ve tried setting the position of another part at the destination which always appears to go in the correct direction but whenever I then print the material it appears to coincide with a different direction.

local RunService = game:GetService("RunService")

function GetTouchingMaterial(part)
	local Location = part.Position + -(part.CFrame.UpVector * 25)
	--workspace.Other.CFrame = CFrame.new(Location)
	local ray = Ray.new(part.Position, part.CFrame:VectorToWorldSpace(Location))
	local _, _, _, material = workspace:FindPartOnRay(ray, part)
	return material
end

function FloorMaterial()
	local Base = script.Parent
	local material = GetTouchingMaterial(Base)
	print(material)
end
RunService.Stepped:Connect(FloorMaterial)

I appreciate any advice, thanks for taking the time to read this!

i think you need to make the upvector positive.
Currently it’s inversed and acting like a “downvector”.

I highly suggest you use the new raycasting instead of the old way:

Try this:


function GetTouchingMaterial(part)
	local Origin = part.Position
	local Direction = part.Position + -(part.CFrame.upVector * 25)
	
	local RayResult = workspace:Raycast(Origin, Direction)
	if RayResult then
		local Material = RayResult.Material
		print(Material)
		return Material
	end
end

It worked:


Prints the Enum

2 Likes

I think he wants to get the material below the part.

Yes, forgot to say. I’m looking for the downVector.

1 Like

You can do so with both. @SpacialEthanRB Good idea, turned out the issue was literally just with: VectorToWorldSpace (I didn’t need it, was a bit of code left over from my last raycast script).

1 Like