Raycasting a turrets laser, struggling to find correct position/direction

Hi there! I’m trying to make a laser turret (pending texture) that chases players with a laser. The focusing on players and chasing them part is done, but I’m attempting to use raycasting to position a beam attachment which stands as the end of the laser and the position of a laserdamage particle, although the raycasting I’m attempting to use is going horribly wrong.

The raycast method I have concocted so far is as follows:

local rayOrigin = script.Parent.LaserPos.Position
local rayDirection = script.Parent.LaserPos.Orientation

local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {script.Parent.LaserPos.Parent}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams)

if (raycastResult == nil) then
	print("nil")
end

if raycastResult then
	
	local hitPart = raycastResult.Instance
	
	if hitPart.Transparency == 0 then
		workspace.t.CFrame = CFrame.new(raycastResult.Position)
	end
end

I made it using this guide.

The raycast intersection position point is t which is selected using the move tool to make its position more obvious.

The issues are that the position the raycast is giving as an intersection is under the baseplate and in the wrong place, the raycast doesn’t detect half the circumference around it (which is when the console prints nil), instead of the raycast position moving around with the player, instead it moves in an exponential accelerating line, and when the raycast is left for a while (it repeats every few milliseconds), it slowly descends which you can see at the start of the video.

I assume this is something to do with the direction that I’ve set the raycast to look, but I don’t know what else to set it to. Any help would be greatly appreciated!

Video of the issue

1 Like

Do you think there’s any way you can simplify what you were saying? What is “LaserPos”, also, do you want the RayCast to ignore the “t” part in-case it may be getting in the way?

Hi, thanks for responding! LaserPos is the part on the turret where the beam fires from, since it’s not textured yet, on the video it looks like a small part at the top of the beam laser. From the little experiments I’ve been running, t has yet to get in the way, but I’ve added it to the blacklist anyway, thanks for that lol.

Simplify attempt #1 (I love to over-explain)
LaserPos fires a laser which is supposed to end when it hits the nearest solid material (transparency = 0), and I’ve set up t to be positioned at that intersection point. t is supposed to line up with the beam on the turret, where an infinitely long beam would intersect with the baseplate, but it isn’t instead it’s positioned in strange places inside the baseplate which you can see from the video. I was wondering if anyone knew the cause of this because I’m a little bit lost at this point.

Try making: local rayDirection = script.Parent.LaserPos.CFrame.LookVector * (range number here)
or: local attachment = Instance.new(“Attachment”) attachment.Position = Vector3.new(0,0,range) local rayDirection = attachment.WorldPosition
Using orientation isn’t good to use. i hope this helps