Visualizing Rays

I am currently making a laser beam script (without using a tool) and I am trying to find the best way to visualize a ray.

This is what I have currently

What I am trying to achieve (without the tool)

Whole Script:

local UserInputService = game:GetService("UserInputService")
local hole = script.Parent:WaitForChild("RightArmWeld").Piece
local debris = game:GetService("Debris")
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local nweld = script.Parent.Torso['Right Shoulder']
local arm = script.Parent:WaitForChild("Right Arm")
local animation = script:WaitForChild("animation")
local hum = script.Parent:FindFirstChildWhichIsA("Humanoid")
local anim = hum:LoadAnimation(animation)
local lookvector = hole.CFrame.LookVector

		oldC0 = nweld.C0
		nweld.CurrentAngle = 0
		nweld.DesiredAngle = 0
nweld.MaxVelocity = 0

local equipped = false



UserInputService.InputBegan:Connect(function(input,gameProccesedEvent)
	if input.KeyCode == Enum.KeyCode.E and not gameProccesedEvent then
		equipped = true
		while equipped do
			anim:Play()
		local tframe = script.Parent.Torso.CFrame
			tframe = tframe + tframe:vectorToWorldSpace(Vector3.new(1, 0.5, 0))
			local taim = mouse.Hit.p -( tframe.p )
			nweld.C0 = (CFrame.new(Vector3.new(),tframe:vectorToObjectSpace(taim))*CFrame.Angles(0,math.pi/2,0))+Vector3.new( 1, 0.5, 0 )
			ray = Ray.new(hole.Position, (hole.CFrame.lookVector).unit*500,100,false)
			
			
			
			wait()
		end
	end
end)
UserInputService.InputEnded:Connect(function(input,gameProccesedEvent)
	if input.KeyCode == Enum.KeyCode.E then
		equipped = false
		nweld.MaxVelocity = .15
		nweld.C0 =oldC0
		anim:Stop()
	end
end)




Simplified Script:

local hole = script.Parent:WaitForChild("RightArmWeld").Piece -- hole is a part welded to the right arm
ray = Ray.new(hole.Position, (hole.CFrame.lookVector).unit*500,100,false)

Any help would be greatly appreciated as I am not good at raycasting.

1 Like

Roblox has actually made a very good tutorial on raycasting, and you can check it out here. At the link, check out the dropdown “Calculating Direction From an Origin and Destination” which I am sure answers your question. Keep in mind this tutorial uses the new “RaycastParams” system!

2 Likes

You can do something like this.

local part = Instance.new("Part");
	part.Material = Enum.Material.Neon;
	part.Size = Vector3.new(.2, .2, ray.Direction.magnitude);
	part.CFrame = CFrame.new(ray.Origin + ray.Direction/2, ray.Origin + ray.Direction);
	part.Anchored = true;
	part.CanCollide = false;
	part.Parent = game.Workspace.CurrentCamera; -- or wherever you want to parent it
1 Like

I tried this and got this

to fix the repeating I tried putting the ray out of the loop but it didn’t update the position then I tried putting just the CFrame in the loop but nothing changed.

1 Like

I’ll try making it myself in Studio. But first, I have a question, was the laser beam intended to always be there or appear only when a player clicks to shoot or something like that?
If it would always be there, then welding a part to the hand seems to be a good option.

Sorry for the late reply but basically the laser beam continuously shoots when a player holds down the button “E”. The reason I welded a part to the hand is because I am welding another different part to the hand as sort of a “prop”. This is all in a local script in StarterCharacterScripts. I know it is only local but I intend on making a remote event once it works locally for convenience.

Is this what you wanted? I apologize for the lag :sweat_smile:
robloxapp-20200812-1555409.wmv (817.9 KB)
PS-I’m sorry that you have to download it. I’m actually new to the DevForum, could you tell me how you uploaded the previous videos.

Yes this is what I am looking for. In regards to your other question… To upload a video you go to the upload button image then hit choose files and select your video file.

1 Like

I also made a laser gun not too long ago, and here’s what I used:

lasers.LaserBeam.Size = Vector3.new(lasers.LaserBeam.Size.X, lasers.LaserBeam.Size.Y, (endPoint - bulletHole.Position).magnitude)
local middlePoint = bulletHole.CFrame:Lerp(CFrame.new(endPoint), 0.5)
lasers.LaserBeam.CFrame = CFrame.new(middlePoint.Position, mousePosition)

lasers.LaserBeam is a premade part. I made it’s X and Y value very thin, and all I had to change was the Z value for the length. I used the length of the ray for this.

Then, I found the middle point of the ray and used it as the part’s position.
Finally, I used the direction of the ray as the direction of the part.

local UserInputService = game:GetService("UserInputService")

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local nweld = script.Parent.Torso['Right Shoulder']
local arm = script.Parent:WaitForChild("Right Arm")
--local animation = script:WaitForChild("animation")
local hum = script.Parent:FindFirstChildWhichIsA("Humanoid")
--local anim = hum:LoadAnimation(animation)

local oldC0 = nweld.C0
nweld.CurrentAngle = 0
nweld.DesiredAngle = 0
nweld.MaxVelocity = 0

local equipped = false

local part = Instance.new("Part");
part.Size = Vector3.new(0,0,0)
part.Material = Enum.Material.Neon;
part.Anchored = false;
part.CanCollide = false;
part.Parent = workspace;

local weld = Instance.new("Weld")
weld.Part0 = arm
weld.Part1 = part
weld.C0 = CFrame.new(0,-1 + part.Size.Y/2,0) 
weld.Parent = part

UserInputService.InputBegan:Connect(function(input,gameProccesedEvent)
	if input.KeyCode == Enum.KeyCode.E and not gameProccesedEvent then
		equipped = true
		while equipped do
			--	anim:Play()
			local hole = arm.CFrame + (-arm.CFrame.UpVector * (arm.Size.Y/2))
			local tframe = script.Parent.Torso.CFrame
			tframe = tframe + tframe:vectorToWorldSpace(Vector3.new(1, 0.5, 0))
			local taim = mouse.Hit.p -( tframe.p )
			local mag = math.clamp(taim.Magnitude,0,100)
			nweld.C0 = (CFrame.new(Vector3.new(),tframe:vectorToObjectSpace(taim))*CFrame.Angles(0,math.pi/2,math.pi/2))+Vector3.new( 1, 0.5, 0 )
			part.Size = Vector3.new(0.2,mag,0.2)	
			weld.C0 = CFrame.new(0,-1 - part.Size.Y/2,0) 
			wait()
		end
	end
end)
UserInputService.InputEnded:Connect(function(input,gameProccesedEvent)
	if input.KeyCode == Enum.KeyCode.E then
		equipped = false
		nweld.MaxVelocity = .15
		nweld.C0 =oldC0
		game:GetService("TweenService"):Create(part,TweenInfo.new(0.1),{Size = Vector3.new(0,0,0)}):Play()
		player.Character.Animate.Disabled = false
		--anim:Stop()
	end
end)

I replaced the animation with another rotation of 90 degrees.

(CFrame.new(Vector3.new(),tframe:vectorToObjectSpace(taim))*CFrame.Angles(0,math.pi/2,math.pi/2))+Vector3.new( 1, 0.5, 0 )

Ok I just noticed something, since the part moves along with the arm, it gets weird when the player walks. There should be an easy fix for this.

1 Like

Small issue when the player moves the arm goes up causing the laser to move with it.

1 Like

Lemme try fixing that. 30 characters

Okay I found a fix. If I use the animation it works no problem. Thanks for the help!

1 Like

As a side note if anyone is trying to recreate this use a massless part stored in ReplicatedStorage and clone it or else whenever you aim at the sky you get pushed under the ground.

1 Like