Problem with raycast

i made a raycast from a part that goes all the way down to detect the baseplate, for some reason when i put other parts between it keeps printing the baseplate and doesnot change it as if it detects the part

local ser = runs.Heartbeat:Connect(function()
				task.wait(0.1)
				local pos = ultPart.PrimaryPart.CFrame
				local ray = Ray.new(ultPart.PrimaryPart.CFrame.UpVector * 100, ultPart.PrimaryPart.CFrame.UpVector * -100)
				local result,position = game.Workspace:FindPartOnRayWithIgnoreList(ray,{game.Workspace[plr.Name],game.Workspace.map,game.Workspace.Enemies})
				print(result)
				ultPart.PrimaryPart.CFrame = pos * CFrame.new(0.01,0,0.01)
			end)
local ray = Ray.new(ultPart.PrimaryPart.CFrame.UpVector * 100, ultPart.PrimaryPart.CFrame.UpVector * -100)

Don’t use Ray.new() as it’s deprecated, instead use game:GetService("Workspace"):Raycast() but i digress. The issue here is that you are using a directional vector as the start position for the raycast. Basically, you aren’t using the actual position of the PrimaryPart instead, you are using the Vertical Direction of the PrimaryPart.

You can find great posts that explain the different type of Vectors. As I’m not going to do that in this post.

Two simple fixes for your issue is simply:

Ray.new(ultPart.PrimaryPart.Position, -ultPart.PrimaryPart.CFrame.UpVector * 100)

If you have any confusions regarding the two examples of code i gave you, just ask :)!

EDIT
I decided to just write a solution with the current raycast system. And removed the previous errors in my reply

    local rayOrigin = model.PrimaryPart.Position
    local blacklist = RaycastParams.new()
    blacklist.FilterDescendantsInstances = {model}
    
    local result = workspace:Raycast(rayOrigin , -model.PrimaryPart.CFrame.UpVector * 100)
    if result then
        local hitPosition = result.Position
        print(result.Distance, result.Instance)
       
    end
    model.PrimaryPart.CFrame *= CFrame.new(0.01,0,0.01)
2 Likes

thank you my guy, i appreciate it

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