Better way to go about this?

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
1 Like

It looks like you are aiming from a camera offset, then when the ball is thrown the camera switches back to straight behind the player.
Seems to me the amount it’s throwing to the right would be the equivalent of how much the camera offset flips between the 2 views.

Alright, I’ll take a look into that. Thank you!

So how would I go about fixing this offset, would I just add it to when the ball gets positioned in front of the player? Or is it something to do with the direction

local ballDirection = (hit.Position - player.Character.Head.Position).Unit

Your direction is derived from the hit point and the player’s head. What you actually want is the direction to be derived from the camera / the crosshair point, otherwise the aiming will be inconsistent.

Luckily, you already essentially have that implemented.

UnitRay.Direction

Is exactly what youre looking for. No raycast necessary either.

Another thing to add, you should keep a consistent camera perspective. If your camera goes back to the normal perspective, BEFORE the ball being thrown, the difference between the over-the-shoulder perspective and the normal perspective will make the direction inconsistent.

2 Likes

Tsym! I appreciate the in-depth explanation! This has helped out a ton.

To clarify, is the / saying to divide them? Or just that the crosshair point and camera should be used for direction

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.