How to convert orientation to direction for raycast?

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:

Treklatom Turret Output

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 :slight_smile:

1 Like

Honestly I have such bad memory… But I made something pretty similar placing a part showing the travel of the ray, resizing it, matching the angle, and position.

On each iteration, you have the origin and hit point on the RaycastResult, you can get how long your part should be. Based on the orientation angle of the “turret” you have the orientation angle that the part should have. Based on the origin spot of the ray you can offset the “raypart” according to the size of the raypart, and thats it.

Your Raycast is only 50 studs, is that related with your issue? If nothing is hit how you gonna handle it?

1 Like

Or just Use Look/Up/Right Vector or negative it for the opposite direction.

1 Like

Try replacing the areacast and raycast with this and let me know if it works.

local RayCast = workspace:RayCast(CFrame.new(script.Parent.Position), CFrame.new(0, 0, 1) * Dist * CFrame.Angles(0, math.rad(Orien.Y, 0)
1 Like

Just convert the x y and z axis of said orientation into a cframe.angles and then use lookvector.

1 Like

Problem solved! I found this reply from @Downrest which is shown below:

Sorry, unfortunately it didn’t work

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.