So I’ve asked how to do this a lot, and I’m still kind of confused on how to make a bullet or cannon fire directly where your mouse is pointing.
One problem I always have when I make scripts that are projectiles is the bullet/cannon doesn’t always go where it’s meant to go. Sometimes the projectile will fire in the opposite direction, or just some random location. This has to do with filtering out things that get in the way of your aim most likely, but nobody really explains well enough how to do that.
Another problem is maybe I’m not pointing the projectile where it’s supposed to go correctly before moving it forwards? How do you do this if so?
Here’s an example of how I point my projectiles:
local targetPosition = Array.HitPosition or Array.EndPosition
bullet.CFrame = CFrame.new(bullet.CFrame.p, targetPosition)
Now here’s an example of how I filter the aiming for the projectiles:
Mouse.TargetFilter = workspace
Mouse.Button1Down:Connect(function()
if Player.Character and Player.Character.Humanoid.Health > 0 then
if Tool.Parent == Player.Character then
if id and anim then
local hum = Player.Character.Humanoid
local track = hum:LoadAnimation(anim)
track:Play()
end
local raycastParams = RaycastParams.new()
local Chars = workspace.Chars or workspace:WaitForChild("Chars")
raycastParams.FilterDescendantsInstances = {Chars, Tool, workspace.Bullets, workspace.CurrentCamera}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
local unitRay = camera:ScreenPointToRay(Mouse.X, Mouse.Y)
local raycastResult = workspace:Raycast(unitRay.Origin, unitRay.Direction * length, raycastParams)
Fired:FireServer(
{
HitPosition = raycastResult and raycastResult.Position or nil;
EndPosition = unitRay.Direction * length;
}
)
end
end
end)