Hello, I’m working on a turret and I want it to have a laser pointing from a glowing red light in the middle of the machine. However, when creating a system to change the length of the beam by changing the position of an attachment, I have encountered an issue as I am unable to figure out how to convert the orientation of the red light (a MeshPart) to a Direction for the raycast.
I have searched on the Dev Forum; however, I was only able to find how to convert Direction to Orientation and what I tried didn’t work as it only raycasted towards the baseplate instead of forwards from the part which is shown below:
The issue also occurs at longer distances as shown below:
The beam is too short as because of the raycast, it is using the distance of the part from the baseplate as shown in my output menu:
Here is the code I am currently using if it helps:
while wait() do
local Params = RaycastParams.new() -- Raycast Paramaters
Params.FilterType = Enum.RaycastFilterType.Exclude
Params.RespectCanCollide = true
Params:AddToFilter(script.Parent.Parent:GetDescendants()) -- Excludes the turret model
local Dist = 50 -- Max Distance
local Offset = Vector3.new(0, 0, -Dist) -- Position of the 2nd attachment which will change the beam's length
local Orien = script.Parent.Orientation -- Orientation of the light (MeshPart)
local AreaRay = Ray.new(script.Parent.Position, Vector3.new(0, Orien.Y/90, 0)) -- The ray used in the raycast (I'm unsure how to calculate the 2nd parameter (Direction))
local RayCast = game.Workspace:Raycast(AreaRay.Origin, AreaRay.Direction*Dist, Params)
if RayCast then
if RayCast.Instance then
print(":: Raycast hit object -", RayCast.Instance, "::")
if RayCast.Instance:IsA("BasePart") then
local PosA = RayCast.Position -- Where the raycast hit
local PosB = script.Parent.Position -- The position of the light (MeshPart)
local Dist = (PosA-PosB).Magnitude -- Distance from where the raycast hit
Offset = Vector3.new(0, 0, -Dist)
end
end
end
script.Parent:WaitForChild("Cast").Position = Offset -- Updating the beam's length
end
If anyone knows how I can achieve what I am aiming to then please reply as it would be greatly appreciated.
Have a good day