Ray positionning and direction issues

im trying to cast a ray from an attachment, and everything is working fine but for some reason the ray wont position its origin where the attachment is. I dont have any way to “see” where the ray is so its hard to tell whats wrong. There are no errors in my output

Heres the code

local attach = script.Parent
local RayOrigin = script.Parent.Position

local rayDirection = Vector3.new(0,50,0)
local ray = Ray.new(RayOrigin, rayDirection)

while task.wait(1) do
	local hit = game.Workspace:FindPartOnRayWithIgnoreList(ray, {attach})
	if hit then
		print("Ray was hit by", hit)
	else
		print("Ray was not hit")
	end
end

Its pretty simple, this is my first time raycasting :sweat_smile:

Help would greatly be appreciated, thank you for your time :smiley:

1 Like

The position of an attachment is relative to the center of the block.

Change the origin to

local rayOrigin = part.Position + script.Parent.Position

part being the Parent part of the attachment

image
We have an attachment here. The position of the part is
image

The position property of the attachment is also
image

You notice how they are the exact same positions but the attachment is actually a bit higher than the part’s center.

The reason for that is the position of an attachments is relative to the center of the part. Think of the parts position to be 0,0,0 or the center of the world. The position property is an offset

1 Like

for goodness sakes. Stop using deprecated methods. Use workspace:RayCast(). Also the issue was with the direction. Instead, use .LookVector * distance to shoot infront. Also use WorldPosition instead of Position. And WorldCFrame too.

local attach = script.Parent
local RayOrigin = script.Parent.WorldPosition

local rayDirection = script.Parent.WorldCFrame.LookVector * 100 -- shoots infront the part by 100 studs


while task.wait(1) do
	local hit = workspace:Raycast(RayOrigin, rayDirection)
	if hit then
		print("Ray was hit by", hit.Instance.Name)
	else
		print("Ray was not hit")
	end
end

@Pyromxnia

1 Like

I know this is resolved but for the sake of clarity here’s what you would have needed to do to cast the ray from the attachment’s origin to 50 studs above it.

local ray = Ray.new(RayOrigin, RayOrigin + Vector3.new(0,50,0))

1 Like