Hi guys! I hope you doing very very well! So basically, I am making a raycast system, but my ray seems to work randomly, and I don’t really know why it doesn’t goes to where I want it to go (the mouse). It may be because I’m not positioning correctly the Beam?
Here’s what happens :
What you need to know to understand the script is the following things:
RemoteEvent (FireServer)
My goal is to make the ray go to where my mouse is positioned
The ray starts from the right hand of the character
I really appreciate it if you can give me a hand with this, even if it’s a little one, I’ll really really be grateful, even if it’s the dumbest thing, just don’t mind it and write it down because it can help me.
Thank you for everything and here’s the script (ServerScript):
game.ReplicatedStorage:WaitForChild('VoteForSomeoneWithHands').OnServerEvent:Connect(function(player, mouse, mousePos, rayDirection)
local char = player.Character
local rayOrigin = char.RightHand.Position
local RayOriginHand = char:FindFirstChild('RightHand')
local RayCastParameters = RaycastParams.new()
RayCastParameters.FilterDescendantsInstances = player.Character:GetChildren()
RayCastParameters.FilterType = Enum.RaycastFilterType.Blacklist
-- Cast the ray
local raycastResult = workspace:Raycast(rayOrigin, rayDirection, RayCastParameters)
local distance = rayDirection - rayOrigin
char.BeamPartGuide.Size = Vector3.new(0.1, 0.1, distance)
char.BeamPartGuide.CFrame = CFrame.new(rayOrigin, Vector3.new(raycastResult)) * CFrame.new(0,0, -distance / 2)
RayOriginHand.Beam.Enabled = true
RayOriginHand.BeamEndAttachment.Position = char.BeamPartGuide.Position
if not raycastResult then
raycastResult = {Position = rayDirection}
end
end)
Here’s the LocalScript where you call the RemoteEvent:
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local mousePos
local RayDirection
mouse.Move:Connect(function()
RayDirection = mouse.Hit.Position
mousePos = mouse.Hit
end)
player.CharacterAdded:Connect(function()
--Give the Beam effects to the player
game.ReplicatedStorage:WaitForChild('RemoteEvents').GiveBeamToCharacter:FireServer(player)
end)
while wait(1) do
game.ReplicatedStorage:WaitForChild('VoteForSomeoneWithHands'):FireServer(mouse, mousePos, RayDirection)
end
Thank you for all of your help and have a nice day!
From what I was able to gather just glancing at your code, the RayDirection you are using for your raycast is simply this, correct?
and its exactly the same value you use here right?
if this is the case, then I think i know what you’re doing wrong. You are using the mouse’s position vector as if it was a directional vector, and thats not what you need to use in the workspace:Raycast() function.
To get the direction between two points you substract the Origin position from the final position. Which is a bit ironic cause you did do that, but you named it ‘distance’
i think if you were to use this ‘distance’ value in the raycast it would probably work, but i havent tested it. So like this.
local distance = rayDirection - rayOrigin
local raycastResult = workspace:Raycast(rayOrigin, distance, RayCastParameters)
--switched around
I think most of the confusion is that the definitions seem wrong
You’re doing quite a few things wrong here and I’m pretty sure all of them are playing into your beam not going where it should.
game.ReplicatedStorage:WaitForChild('VoteForSomeoneWithHands').OnServerEvent:Connect(function(player, mousePos)
--// mouse was removed bc it wasn't used and passing it to the server doesn't actually give you the mouse.
--// rayDirection was removed bc you don't need it anymore
local char = player.Character
--local rayOrigin = char.RightHand.Position
--local RayOriginHand = char:FindFirstChild('RightHand')
local playerHand = char:WaitForChild('RightHand') --// using FindFirstChild without checking if it exists is useless.
--// also you should wait for the RightHand before getting the origin but we don't need the origin rn
--local RayCastParameters = RaycastParams.new()
--RayCastParameters.FilterDescendantsInstances = player.Character:GetChildren()
--RayCastParameters.FilterType = Enum.RaycastFilterType.Blacklist
-- Cast the ray
--local raycastResult = workspace:Raycast(rayOrigin, rayDirection, RayCastParameters)
--local distance = rayDirection - rayOrigin
--// you don't need to raycast since you're using a beam which will automatically connect the 2 parts together
char.BeamPartGuide.Size = Vector3.new(0.1, 0.1, (playerHand.Position - mousePos).Magnitude) --// to get the distance between 2 vectors you subtract the Start from the End and get the length of it(.Magnitude)
--char.BeamPartGuide.CFrame = CFrame.new(rayOrigin, Vector3.new(raycastResult)) * CFrame.new(0,0, -distance / 2)
char.BeamPartGuide.CFrame = CFrame.new(mousePos) --// since beams will automatically draw a line between 2 parts we just need the beam guide to be at the end
playerHand.Beam.Enabled = true
--RayOriginHand.BeamEndAttachment.Position = char.BeamPartGuide.Position --// the position of a attachment is actually the offset from its parent part
--// since its a offset and not its actual position having it just be 0,0,0 puts it at the parts position.
--if not raycastResult then
-- raycastResult = {Position = rayDirection} --// what even is this? if raycastResult didn't exist then your code would've errored a few lines ago
--// yea just remove this it does nothing
--end
end)
Local Script:
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local mousePos
--local RayDirection --// not needed
mouse.Move:Connect(function()
--RayDirection = mouse.Hit.Position --// this isn't needed
mousePos = mouse.Hit
end)
player.CharacterAdded:Connect(function()
--Give the Beam effects to the player
game.ReplicatedStorage:WaitForChild('RemoteEvents').GiveBeamToCharacter:FireServer(player) --// you should be listening to .CharacterAdded on the server but I'll leave it
end)
while task.wait(1) do --// this doesn't relate to your problem but use task.wait() its much better than wait()
game.ReplicatedStorage:WaitForChild('VoteForSomeoneWithHands'):FireServer(mousePos) --// instead of sending over the mouse and the incorrect rayDirection we just send the mouses location and let the server handle it
end
Also @7z99 mouse.UnitRay gets the direction from the camera to the mouse so using it here won’t work