Hello everyone, so i’m trying to create a beam effect that sets the curves depending of the player position.
This is what i mean:
How would i handle this? This is my current script:
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local maxDistance = 18
local function CreateCurvedBeam(startPos, endPos, color)
workspace.ClientStuff:ClearAllChildren()
local sphere = Instance.new("Part")
sphere.Shape = Enum.PartType.Ball
sphere.Color = color
sphere.Position = endPos
sphere.Size = Vector3.new(0.5, 0.5, 0.5)
sphere.Anchored = true
sphere.CanCollide = false
sphere.Parent = workspace.ClientStuff
local midpoint = (startPos + endPos) / 2
local distance = (endPos - startPos).Magnitude
local curveHeight = math.sin(distance / maxDistance * math.pi) * 5
midpoint = midpoint + Vector3.new(0, curveHeight, 0)
local sphereAttachment = Instance.new("Attachment")
sphereAttachment.Position = Vector3.new(0, 0, 0)
sphereAttachment.Parent = sphere
local playerTorsoAttachment = Instance.new("Attachment")
playerTorsoAttachment.Position = Vector3.new(0, 0, 0)
playerTorsoAttachment.Parent = Player.Character.HumanoidRootPart
local beam = Instance.new("Beam")
beam.Attachment0 = playerTorsoAttachment
beam.Attachment1 = sphereAttachment
beam.Parent = workspace.ClientStuff
beam.Color = ColorSequence.new(color)
beam.Width0 = 0.6
beam.Width1 = 0.6
beam.FaceCamera = true
beam.Enabled = true
beam.LightEmission = 1
beam.CurveSize0 = midpoint.Y
beam.CurveSize1 = -midpoint.Y
beam.Width0 = .6
beam.Width1 = .6
end
Mouse.Button1Down:Connect(function()
local playerPosition = Player.Character.HumanoidRootPart.Position
local mousePosition = Mouse.Hit.Position
local direction = (mousePosition - playerPosition).unit * maxDistance
local ray = Ray.new(playerPosition, direction)
local Params = RaycastParams.new()
Params.FilterType = Enum.RaycastFilterType.Exclude
Params.FilterDescendantsInstances = {Player.Character}
local raycastResult = workspace:Raycast(playerPosition, direction, Params)
if raycastResult then
local distance = (raycastResult.Position - playerPosition).Magnitude
if distance <= maxDistance then
CreateCurvedBeam(playerPosition, raycastResult.Position, Color3.fromRGB(255, 0, 0))
else
print("Click is too far away")
end
else
print("Ray did not hit anything")
end
end)
Any help would be appreciated.