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.
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!
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.
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 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 then hit choose files and select your video file.
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.
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.