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)
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.
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.
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.