I’m currently working on a dodgeball system and I am struggling to figure out a more effective way on getting this to work with raycasting rather than mousePos due to crossplay. The issue I am having right now is in the footage you can see the ball go more towards the right rather than center when thrown. If anybody has any advice on what I could do to improve this system I would love the input. Thanks in advance.
Throwing
ballThrow.OnServerEvent:Connect(function(plr, mousePos, throwingPower, ballDirection)
if ballThrown == false then
local plrTeam = plr.Character:GetAttribute("DodgeballTeam")
ballThrown = true
local ball = Instance.new("Part")
ball.Shape = Enum.PartType.Ball
ball.Massless = false
ball.Size = Vector3.new(1.6,1.6,1.6)
ball.Material = Enum.Material.Sand
ball.Color = Color3.new(0.768627, 0.156863, 0.109804)
ball.Name = "Dodgeball"
print(plrTeam)
ball.CollisionGroup = plrTeam.."Ball"
ball.Parent = game.Workspace
local ballScript = script.Parent.Parent.ballScript:Clone()
ballScript.Parent = ball
ballScript.Enabled = true
ball.CFrame = plr.Character.HumanoidRootPart.CFrame + (plr.Character.HumanoidRootPart.CFrame.LookVector * 2)
ball.Velocity = ballDirection * throwingPower
local throwerValue = Instance.new("ObjectValue")
throwerValue.Parent = ball
throwerValue.Name = "Thrower"
throwerValue.Value = plr
script.Parent.Parent:Destroy()
end
end)
Script that gets new mousePos
local function raycast()
local camera = workspace.CurrentCamera
local cameraCF = camera.CFrame
local length = 500
local params = RaycastParams.new()
local middle = camera.ViewportSize / 2
params.FilterType = Enum.RaycastFilterType.Exclude
params.FilterDescendantsInstances = {player.Character, player:FindFirstChild("ClassicSuperball")}
local unitRay = camera:ViewportPointToRay(middle.X, middle.Y)
local hit = workspace:Raycast(
unitRay.Origin,
unitRay.Direction * length,
params
)
local ballDirection = (hit.Position - player.Character.Head.Position).Unit
return ballDirection, unitRay.Origin
end