Raycasting......... help

so apparently i’ve done alot of research and i did quite a bit of experiment but i am still unable to achieve what i needed to.

what i actually need to achieve is that the intersection point of the raycast getting the position of the area where the raycast stopped hitting not getting the part’s position

i used raycast.Position but instead it just got the direction end position i want like to get the raycasts hit position. if my explanation is not understandable i wish to achieve this:

image

3 Likes

I know how to do this, but I sorta forgot how. I’ll hop on studio and make a quick raycast and tell you when I can. Sorta can’t get on studio rn.

Yeah that would be Raycast.Position. What’s wrong with it?

local raycast = workspace:RayCast(Vector3.new(0,5,0),Vector3.new(0,-10,0))
if raycast then
    print(raycast.Position) -- this would return the raycasts hit position and not raycasts instance position
    print(raycast.Instance.Position) -- this would return raycasts hit instance position
end
3 Likes

Raycast.Position doesn’t get the end position of the ray or the position of the hit instance.

1 Like

If you want to get the exact position where a Raycast hit a part, you should use the Hit property of the RaycastResult. The Hit property contains information about the instance (like the part) that was hit and the position of the hit.

local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
raycastParams.FilterDescendantsInstances = {workspace.IgnoreRaycast}

local ray = Ray.new(startPosition, direction)

local hitResult = workspace:Raycast(ray, raycastParams)

if hitResult then
    local hitPart = hitResult.Instance
    local hitPosition = hitResult.Position
    
    print("Hit part:", hitPart)
    print("Hit position:", hitPosition)
else
    print("Ray didn't hit anything.")
end

Maybe try RaycastResult.Normal? I know that returns a vector3 that might be what you want.


If are confused on how the direction parameter works on the Raycast function this is how it works

local startPosition = Vector3.new(10, 5, 0) 
local goalPosition = Vector3.new(0, 5, 0)

local direction = goalPosition - startPosition 

local result = workspace:Raycast(startPosition, direction)

if result then
       print("Result Position:", result.Position)
end

The normal is a unit vector, pointing outwards from the intersected face. I have no clue what OP is looking for, but I’d wager to say that probably isn’t it.

i did some script testing and this is my script:

local StartPos = (workspace.Part.CFrame.LookVector * 20) + workspace.Part.Position
local Direction = Vector3.new(0,-40,0)

local RayParams = RaycastParams.new()
RayParams.FilterDescendantsInstances = {workspace.Part,workspace.arf}
RayParams.FilterType = Enum.RaycastFilterType.Blacklist
RayParams.IgnoreWater = true

local Raycast = workspace:Raycast(StartPos,Direction,RayParams)

if Raycast then
	workspace.arf.CFrame = CFrame.new(Raycast.Position)
end

result:


why is it under? it suppose to be like over above…

here’s another one testing i did one almost near the block and it turned out like this:

image
aint it supposed to do this?

also im wondering like after getting the part that the raycast intersected with how do i get like the surface of what i hit like for example it hit the right then it would print as “hit surface right”

I believe you’re confused about how a Raycast works, as Raycast requires vectors, not a cframe.

local start = workspace.start
local direction = Vector3.new(0, 0, -45)

local visualiser = Instance.new("WireframeHandleAdornment")
visualiser.Color3 = Color3.fromRGB(137, 53, 255)
visualiser.Adornee = workspace.Terrain
visualiser.Parent = workspace

local rayParams = RaycastParams.new()
rayParams.IgnoreWater = true

local result = workspace:Raycast(start.Position, direction, rayParams)

if result then
	visualiser:AddLine(start.Position, result.Position)
end

I would recommend learning about CFrames and Vectors before fully going onto Raycasts. Originally you were adding extra positions and making the origin position further away.

1 Like

although your explanation kind of helped me abit, but i just found the way entirely by myself. thanks for your help and all the other who have tried to help!

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