BodyForce applied to object in the wrong direction?
Hi, I'm trying to make a script that throws a ball in the direction the player clicked. What I have so far is
Local Script:
01 | local player = game.Players.LocalPlayer |
02 | local mouse = player:GetMouse() |
03 | local mousepos = mouse.Hit.p |
04 | local handle = script.Parent |
05 | local tool = handle.Parent |
07 | local RS = game:GetService( "ReplicatedStorage" ) |
08 | local ThrowBallEvent = RS:WaitForChild( "ThrowBall" ) |
10 | tool.Activated:connect( function () |
11 | ThrowBallEvent:FireServer(tool, mousepos) |
Server Script:
01 | local RS = game:GetService( "ReplicatedStorage" ) |
02 | local ThrowBallEvent = RS:WaitForChild( "ThrowBall" ) |
04 | local function ThrowBall(player, tool, mousepos) |
06 | local ball = tool:clone() |
07 | ball.Parent = game.Workspace |
09 | local rootpart = player.Character:FindFirstChild( "HumanoidRootPart" ) |
10 | local humanoid = player.Character:FindFirstChild( "Humanoid" ) |
11 | local head = player.Character:FindFirstChild( "Head" ) |
13 | local newhandle = ball:WaitForChild( "Handle" ) |
14 | newhandle.Position = head.Position + head.CFrame.lookVector * 4 |
15 | newhandle.CanCollide = true |
17 | local force = Instance.new( "BodyForce" , newhandle) |
18 | local targetpos = mousepos |
19 | local initialpos = rootpart.Position |
20 | local direction = (mousepos - initialpos).unit |
21 | force.Force = direction * 300 |
25 | ThrowBallEvent.OnServerEvent:Connect(ThrowBall) |
The ball is thrown, but the direction of the force is completely arbitrary. Sometimes the ball will take off to the left, sometimes it'll go behind me, and sometimes it'll go straight. Any idea on why it's acting like this and not just going where I click?