How to raycast straight forward on a moving part

So I have a moving part that I want a raycast to go straight forward on even if the part rotates, I am new to raycasting and wondering how I could go about this?

Use the LookVector of their CFrame

local part = --Your part

local Result = workspace:Raycast(part.Position,part.CFrame.LookVector * 10) --Raycast 10 studs forward from part

if Result then
	print("Ray detected part")
end

I have tried that and it results in the ray going ~0,0,0
Screenshot_911

...CFrame.LookVector*10)

What is your code right now and how does the part look like in-game when testing

local car = script.Parent.Parent.Parent
local blacklist = {}

for i,v in pairs(car:GetDescendants()) do
	if v:IsA("BasePart") then
		table.insert(blacklist, v)
	end
end

local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = blacklist
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist

wait(15)

print("Loaded AI")

function fireRay(position, direction)
	local ray = workspace:Raycast(position, direction, raycastParams)
	
	local beam = Instance.new("Beam")
	local a0 = Instance.new("Attachment")
	local a1 = Instance.new("Attachment")
	beam.Enabled = false
	a0.Parent = car.Body.Front
	a1.Parent = car.Body.Front
	a1.Name = "Attachment1"
	beam.Parent = car.Body.Front
	a0.WorldPosition = position
	a1.WorldPosition = direction
	beam.Attachment0 = a0
	beam.Attachment1 = a1
	beam.Color = ColorSequence.new(Color3.fromRGB(0,255,0))
	beam.Width0 = 0.1
	beam.Width1 = 0.1
	beam.FaceCamera = true
	beam.Enabled = true
	
	if ray then
		print("Hit: ".. ray.Instance:GetFullName())
		wait(0.1)
		a0:Destroy()
		a1:Destroy()
		beam:Destroy()
		return true
	else
		print("Hit nothing")
	end
	
	wait(0.1)
	a0:Destroy()
	a1:Destroy()
	beam:Destroy()
end

game:GetService("RunService").Stepped:connect(function()
	local ray = fireRay(car.Body.Front.Position, car.Body.Front.CFrame.LookVector*10)
	if ray then
		script.Parent.Values.Throttle.Value = 0
		script.Parent.Values.Brake.Value = 1
	else
		script.Parent.Values.Throttle.Value = 1
		script.Parent.Values.Brake.Value = 0
	end
end)

https://gyazo.com/e6c8c1833d5ecece471e5147f86dfbf4

Note: This is just to see if I can get it to work properly

That’s odd, it should work as even the official Roblox dev wiki does that, I’m confused how it’s going that far when you set it to 10. I think maybe the beam is causing isses? Maybe visualise it via a part instead of a beam?

It’s because the direction only has a magnitude of 1. Since you are setting world position of the attachment it’s thinking it needs to be within 1 stud of the origin. So just the attachment is in the wrong place.

The thing is that if I put a part right where the beam is it will say that the raycast was hit so the beam isnt wrong. Or it did now it doesnt this is the most broken thing I have ever expierenced.
@tlr22

You should be setting a1.WorldPosition to position + direction

It still doesn’t detect hitting the wall…

Well that fixes the visualization and now its showing to the left and not straight which would explain why when I put a part there it stopped but won’t on a wall
Screenshot_912

The fix was to use rightvector(I already know the different vectors from making TweenService doors)

Thanks for the help @hell_ish @EmbatTheHybrid @tlr22

3 Likes