Log in to vote

How can I make a gun work in a bird's eye view?

Asked by
KingDomas 153
1 month ago

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 ="Tool")
gun.Name = "BirdsEyeGun"

-- create handle part
local handle ="Part")
handle.Name = "Handle"
handle.Parent = gun
handle.Size =, 3, 5)

-- create barrel part
local barrel ="Part")
barrel.Name = "Barrel"
barrel.Parent = handle
barrel.Size =, 1, 3)
barrel.Position =, 1.5, 3)

-- create firing sound
local firingSound ="Sound")
firingSound.Name = "FiringSound"
firingSound.SoundId = "rbxassetid://20369167"
firingSound.Parent = gun

-- create bullet object
local bullet ="Part")
bullet.Name = "Bullet"
bullet.Size =, 0.5, 2)
bullet.BrickColor ="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 *, 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

-- add firing function to gun's equipped event
    -- play firing sound when left mouse button is clicked
        if inputObject.UserInputType == Enum.UserInputType.MouseButton1 then

-- add gun to the game's tool list
gun.Parent = game:GetService("StarterPack")

What should I do to fix my gun script?

1 answer

Log in to vote
Answered by
blowup999 659 Moderation Voter
1 month ago

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

If you set the script to automatically face the player in the direction the mouse is pointing, you can just use the barrel.CFrame.LookVector instead of calculating that. blowup999 659 — 1mo

Answer this question