Roblox raycating

I know this has been asked before but i literally just don’t understand

here’s the important snippet of my pathfinding code (everything else is set up correctly):

local function FindTarget()
	local rayparams = RaycastParams.new()
	rayparams.FilterDescendantsInstances = {root.Parent}
	rayparams.FilterType = Enum.RaycastFilterType.Exclude
	local ray = workspace:Raycast(root.Position, Vector3.new(100, 10, 200), rayparams)
	print(ray)
end

the part is facing the right way. it’s just printing “part” (there are several instances named part so it makes sense but even when the player is in front nothing new is printed)

I know this topic may have been asked before, but please tell us what you exactly want to do. I’ll go over some possible solutions, but please include details, especially for new people to the topic like me.

I’m not sure if this is the angle you want, but if you want your raycast to pick up parts in front of the block, you should use this instead:

local ray = workspace:Raycast(root.Position, root.CFrame.LookVector, rayparams)

And if you want to extend the raycast’s length, use this (change length to the number you want)

local ray = workspace:Raycast(root.Position, root.CFrame.LookVector * length, rayparams)

Before: (the blue line is where the part is facing, and the red part is where the raycast is going)
image
After:
image

Now onto this part:

Raycasts return a RaycastResult, which contains either a BasePart or a Terrain cell. To access whatever part or cell was hit , you would do ray.Instance. Furthermore, there may not be a part or cell that it hits, returning nil. To print the part name (if that’s what you want), you would do this:

local hit = ray and ray.Instance or nil -- ray.Instance is set to hit if it is not nil
if hit then
    print(hit.Name)
end

If you want to learn more about raycasts you can check it’s documentation.

And one more thing:

Part of your code may seem important enough that it’s the only thing you should show, however in most cases you should show your entire code, even separate scripts so others can understand them properly. In this situation for me at least you didn’t need to include all your code, but next time, please do.

Sorry if this was too large of a reply for your liking, but I hope I helped you out in the end.

1 Like

this fixed it, though it doesn’t seem to print any limb of the player unless i get really close. the rest of the code is pathfinding related and in this case useless, there’s just a few variables like the humanoid, root and player root.

Raycasting doesn’t have an exact shape. It’s more like a thin line. If you want to use shapecasting or blockcasting to detect more parts go ahead. I’m pretty sure it works similar to raycasting. I don’t know much about it, but you can also read it’s documentation with the link I provided you already.

2 Likes

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