Hello!
I’m making a 2D shooter game and using the FastCast module for my weapon systems.
My weapons only fire at the x, y-axis, and the z-axis always = 0
But when I test my weapon, the bullet is very inaccurate(picture below)
local tool = script.Parent
local player = game:GetService("Players").LocalPlayer
local mouse = player:GetMouse()
local FireEvent = tool:WaitForChild("FireEvent")
tool.Activated:Connect(function()
local MousePositionX = mouse.hit.Position.X
local MousePositionY = mouse.Hit.Position.Y
local MousePosition = Vector3.new(MousePositionX, MousePositionY, 0)
FireEvent:FireServer(MousePosition)
end)
Server:
local tool = script.Parent
local FireEvent = tool.FireEvent
local FastCast = require(tool.FastCastRedux)
local FirePoint = tool.Handle.FirePoint
local Bullet = game.ReplicatedStorage.Bullet
FastCast.VisualizeCasts = true
local caster = FastCast.new()
local BulletFolder = workspace.BulletFolder
local BulletTemplate = Bullet:Clone()
local FastCastParam = RaycastParams.new()
FastCastParam.FilterType = Enum.RaycastFilterType.Blacklist
FastCastParam.IgnoreWater = true
local CastBehavior = FastCast.newBehavior()
CastBehavior.AutoIgnoreContainer = false
CastBehavior.RaycastParams = FastCastParam
CastBehavior.CosmeticBulletContainer = BulletFolder
CastBehavior.CosmeticBulletTemplate = BulletTemplate
local function OnEquipped(player)
FastCastParam.FilterDescendantsInstances = {tool.Parent, BulletFolder}
end
local function OnLengthChanged(cast, lastPoint, direction, length, velocity, bullet)
if bullet then
local bulletLength = bullet.Size.Z/2
local offset = CFrame.new(0, 0, -(length-bulletLength))
bullet.CFrame = CFrame.lookAt(lastPoint, lastPoint+direction):ToWorldSpace(offset)
end
end
local function onRayHit(cast, result, velocity, bullet)
local hit = result.Instance
local character = hit:FindFirstAncestorWhichIsA("Model")
if character and character:FindFirstChild("Humanoid") then
character.Humanoid:TakeDamage(20)
end
game:GetService("Debris"):addItem(bullet, 2)
end
local function fire(player, MousePosition)
if tool.Parent:IsA("Backpack") then return
end
local origin = FirePoint.WorldPosition
local direction = (MousePosition-origin)
local FireSound = tool.Handle.Firesound
caster:Fire(origin,direction, 350, CastBehavior)
FireSound:Play()
end
FireEvent.OnServerEvent:Connect(fire)
tool.Equipped:Connect(OnEquipped)
caster.LengthChanged:Connect(OnLengthChanged)
caster.RayHit:Connect(onRayHit)