Projectile not accurate

I’m trying to use BodyVelocity to make a projectile go where the mouse is, but the projectile isn’t accurate.

Here’s the script that fires the ball.

local rp = game:GetService("ReplicatedStorage")
local Blast = rp:WaitForChild("Blast")

local Ball = script:WaitForChild("Ball")
local Speed = Ball.Speed.Value

Blast.OnServerEvent:Connect(function(Player,direction)
local Character = Player.Character
local HRP = Character:WaitForChild("HumanoidRootPart")

local Projectile = Ball:Clone()
local BodyVelocity = Projectile.PrimaryPart.BodyVelocity
Projectile:SetPrimaryPartCFrame(CFrame.new(HRP.Position))
BodyVelocity.Velocity = direction.lookVector * Speed
Projectile.Parent = workspace
end)

Is there a speed value? Or is it just speed. Also I respect your avatar

Speed is using a NumberValue, this is the hierarchy.
hierarchy

That makes more since. I thought it was in another script.

The project if anyone needs it.
Projectile.rbxl (28.8 KB)

What is direction? Can you show the block of code where this function is called?

Velocity is probably changing if the Ball is unanchored.

I’ve had this same problem before, it’s actually surprising just how simple it is to fix this.

all you have to do is:

Ball.CFrame = CFrame.LookAt(Ball.Position, Mouse.Hit.Position)

BodyVelocity.Veolcity = Ball.CFrame.LookVector * 10 —Times it by how fast you want the ball to go.

Hope this helped!

1 Like

Don’t you mean:

Mouse.Hit.Position

Yes sorry, it was a typo. I’m on my phone so it’s kinda hard to type.

1 Like

Sorry about that, this is the local script that activates the script above.“direction” is Mouse.Hit.

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()


local rp = game:GetService("ReplicatedStorage")
local Blast = rp:WaitForChild("Blast")

local UIS = game:GetService("UserInputService")
local debounce = false
local cooldown = 2

UIS.InputBegan:Connect(function(input,isTyping)
	if isTyping then
		return
	elseif input.KeyCode == Enum.KeyCode.Q then
		if debounce == false then
			debounce = true
			Blast:FireServer(Mouse.Hit)
			wait(cooldown)
			debounce = false
		end
	end
end)

For what it’s worth, here is a more direct method that is less resource intensive than using CFrames.
Direction = End - Start
So in a one-dimensional example, let’s say I start and 7 and want to end at 13. 13-7 = 6 so the direction is positive or +1 and the distance is 6.
To implement this in your code, you would do this.

BodyVelocity.Velocity = (direction.p - HRP.Position).Unit * Speed

Using .Unit gives us a direction that has a length of only one. If .Unit were not included, it would still function however the speed would increase drastically depending on how far away the target was.

1 Like