I'm making a game where you are in a bird's eye view (image). In this game, you are supposed to be able to shoot weapons. However, I'm struggling to get this core concept down, as I'm not sure how to create a gun script which works in this fashion. I have a gun script with some fundamentals down, but it's not working, and I'm not sure how to adapt it for it to work in this case. The bullets should ignore the Y position of the mouse and travel straight towards where it is aimed based on its X and Z values.
My current gun script which, mind you, doesn't work:
-- create gun object local gun = Instance.new("Tool") gun.Name = "BirdsEyeGun" -- create handle part local handle = Instance.new("Part") handle.Name = "Handle" handle.Parent = gun handle.Size = Vector3.new(1, 3, 5) -- create barrel part local barrel = Instance.new("Part") barrel.Name = "Barrel" barrel.Parent = handle barrel.Size = Vector3.new(1, 1, 3) barrel.Position = Vector3.new(0, 1.5, 3) -- create firing sound local firingSound = Instance.new("Sound") firingSound.Name = "FiringSound" firingSound.SoundId = "rbxassetid://20369167" firingSound.Parent = gun -- create bullet object local bullet = Instance.new("Part") bullet.Name = "Bullet" bullet.Size = Vector3.new(0.5, 0.5, 2) bullet.BrickColor = BrickColor.new("Really red") bullet.Material = Enum.Material.Neon bullet.Transparency = 0.5 bullet.CanCollide = false -- create bullet firing function function fireBullet() -- get the current camera's CFrame local cameraCFrame = workspace.CurrentCamera.CFrame -- set the bullet's position to the barrel's position and move it slightly forward bullet.CFrame = barrel.CFrame * CFrame.new(0, 0, 2) -- set the bullet's velocity in the direction of the camera's look vector bullet.Velocity = cameraCFrame.lookVector * 10 -- set the bullet's parent to the workspace bullet.Parent = workspace -- destroy the bullet after 5 seconds wait(5) bullet:Destroy() end -- add firing function to gun's equipped event gun.Equipped:Connect(function() -- play firing sound when left mouse button is clicked game:GetService("UserInputService").InputBegan:Connect(function(inputObject) if inputObject.UserInputType == Enum.UserInputType.MouseButton1 then firingSound:Play() fireBullet() end end) end) -- add gun to the game's tool list gun.Parent = game:GetService("StarterPack")
What should I do to fix my gun script?
The script currently uses camera.LookVector - that's an ok assumption for FPS, for birdseye though, you need to get:
local cf = CFrame.lookAt(LocalPlayer.Character.WorldPivot.Position, mouse.Hit.Position - mouseHitYComponent + plrYComponent)
Then use that cf.LookVector instead of the camera's