workspace:Raycast() not respecting local orientation

Rayasting from the player’s head part doesn’t seem to be working, the Raycast orientation is incorrect. What am I doing wrong here? The “light” part should be updating when there’s a Raycast hit, but it’s definitely not casting properly.

local light = workspace:WaitForChild("Part",1) or game:GetService("ReplicatedStorage"):WaitForChild("Part")
light.Parent = workspace

local param = RaycastParams.new()
param.FilterDescendantsInstances = {script.Parent:GetChildren(),light}
param.FilterType = Enum.RaycastFilterType.Blacklist

local head = script.Parent:WaitForChild("Head")

while task.wait(.1) do
	local source = head.CFrame
	local result = workspace:Raycast(source.Position, source.LookVector * Vector3.new(0, 0, 100), param)
	if result then
		local distance = result.Distance
		light.Size = Vector3.new(light.Size.X, light.Size.Y, distance)
		light.CFrame = CFrame.lookAt(source.Position + Vector3.new(0,0,-distance/2), source.LookVector * Vector3.new(0, 0, -distance/2))
	end
end

1 Like

Instead of using :GetChildren(), Just use script.Parent, You’re retrieving the Children, This is may be the reason of why it’s not working.

1 Like

The raycast functions identically, as the filtered instances may be in a redundant form, they’re still valid instances that filter the same. The problem is the orientation of the raycast seemingly staying the same, but I’m not sure why.

1 Like

I created a red, neon sphere part, at the world origin position 0,0,0.

Pausing the simulation at any time, once the raycast has completed at least once, if I select the part and resize it…

what?!

It aims directly at position 0,0,0

Now this is odd.

you’re using CFrame.LookAt wrong
first, the position ignores the head’s rotation, and the lookAt Vector3 is a point to look at, not a direction

I’d instead set the light’s cframe to Head.CFrame * CFrame.new(0,0,-distance/2)

1 Like

Modifying the part CFrame to your suggestion fixed the part’s problem, but the raycast still doesn’t seem to be casting at the proper orientation.

I don’t think I see an issue in the raycast
how does the light part look from above now?

when it’s not colliding with anything

1 Like

Being in close proximity to spawn, it works as intended.

image

If I scale the part down to half a stud thick, and make it .5 Transparency, so we can see what happens from first person perspective, and I move around…

it’s not perfectly aligned with your canera because of lag and character animations
it looks like maybe the wait(0.1) and/or handling the cframing on serverside (and making you wait for your ping) is causing delays

for the light not updating while you’re on the side of the building, maybe you didnt set all of the building’s parts to CanQuery=true?

you could try using the output to print result each iteration too

1 Like

This is ran using a local script, nothing server-side, the structure is only made from basic parts, and the raycast localscript is the only script. CanQuery isn’t available in properties anymore, oddly. EDIT I disabled CanCollide, when CanQuery became visible, it was already enabled for all the parts.
image

image

Here’s the output for the raycast result, and its distance.

It would probably be more appropriate if I showed the output while the game runs in a video clip:

try multiplying lookVector by 100, not by a Vector3 that multiplies the Z by 100, and multiplies the X and Y by 0

2 Likes

e.g.,
Vector3.new(1,2,3) * Vector3.new(0,0,100) is Vector3.new(0,0,300)
Vector3.new(1,2,3) * 100 is Vector3.new(100,200,300)

1 Like

This is correct, funny how no one else pointed it out.

1 Like

Ayo that was it, look at this…

I was convinced it was an engie bug… But there they were, all the heroes who commented. Thank you all for helping me figure this out.

As a reward, all of the users here get a free commission of your choice. Aside from the most advanced scripting, I can literally do anything involved with game development, rather well. Drop me a message, and name your prizes.

1 Like