How do I fix bullet not shooting towards middle of screen

IM USING FASTCAST

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want the bullets final destination to be the center of the screens position/crosshair

  2. What is the issue? Include screenshots / videos if possible!
    The bullet is not centerd

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub? I looked at other peoples threads on how they want to acheive raycasting from the middle of the screen instead of

mouse.pos

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

LocalScript

local camera = game.Workspace.CurrentCamera
local viewportSize = camera.ViewportSize
local centerScreen = Vector2.new(viewportSize.X / 2, viewportSize.Y / 2)

ServerScript

LocalSignal.OnServerEvent:Connect(function(Plr, Center)
	local startPoint = Muzzle.WorldPosition
	local sound = Muzzle:FindFirstChild("Fire"):Clone()
	local shell = Arm.Waste.Shell:Clone()
	sound.Parent = Muzzle
	sound:Play()
	debris:AddItem(sound, sound.TimeLength + 0.1)
	Muzzle.Flash:Emit(1)
	Muzzle.flashLight.Enabled = true
	cast:Fire(startPoint, Center * Arm.Configuration:GetAttribute("Range"), Velocity, RayCastBehavior)
	wait(0.05)
	Muzzle.flashLight.Enabled = false
	shell.Parent = Arm.Waste.clones
	shell.Transparency = 0
	shell.CanCollide = true
	shell.WeldConstraint:Destroy()
	debris:AddItem(shell, 5)
end)

If you need any other portion of the script or have any other questions please let me know. Thanks!

2 Likes

here:

LocalScript

local camera = game.Workspace.CurrentCamera
local viewportSize = camera.ViewportSize
local centerScreen = Vector2.new(viewportSize.X / 2, viewportSize.Y / 2)

-- Connect to the input service to detect when the player fires the weapon
game:GetService("UserInputService").InputBegan:Connect(function(input, isTyping)
    if not isTyping and input.UserInputType == Enum.UserInputType.MouseButton1 then
        -- Calculate the direction of the raycast based on the camera's position and orientation
        local ray = Ray.new(camera.CFrame.Position, (camera:ViewportPoint(centerScreen) - camera.CFrame.Position).Unit * Arm.Configuration:GetAttribute("Range"))

        -- Fire the raycast using FastCast
        FastCast:Fire(ray, Velocity, RaycastParams, RayCastBehavior)

        -- Play sound effects, create visual effects, etc.
        -- ...
    end
end)

ServerScript

LocalSignal.OnServerEvent:Connect(function(Plr, Center)
    local startPoint = Muzzle.WorldPosition
    local sound = Muzzle:FindFirstChild("Fire"):Clone()
    local shell = Arm.Waste.Shell:Clone()
    sound.Parent = Muzzle
    sound:Play()
    debris:AddItem(sound, sound.TimeLength + 0.1)
    Muzzle.Flash:Emit(1)
    Muzzle.flashLight.Enabled = true
    
    -- Calculate the direction of the raycast based on the camera's position and orientation
    local camera = Plr.Character.Head.Camera
    local viewportSize = camera.ViewportSize
    local centerScreen = Vector2.new(viewportSize.X / 2, viewportSize.Y / 2)
    local ray = Ray.new(camera.CFrame.Position, (camera:ViewportPoint(centerScreen) - camera.CFrame.Position).Unit * Arm.Configuration:GetAttribute("Range"))

    cast:Fire(startPoint, ray.Direction, Velocity, RayCastBehavior)
    wait(0.05)
    Muzzle.flashLight.Enabled = false
    shell.Parent = Arm.Waste.clones
    shell.Transparency = 0
    shell.CanCollide = true
    shell.WeldConstraint:Destroy()
    debris:AddItem(shell, 5)
end)
2 Likes

ill test it out. Thanks for the Response!

2 Likes

on camera:ViewportPoint did you mean ViewportPointToRay?

1 Like

i meant ViewportPointRay to calculate the the direction of the raycast.

local camera = game.Workspace.CurrentCamera
local viewportSize = camera.ViewportSize
local centerScreen = Vector2.new(viewportSize.X / 2, viewportSize.Y / 2)

-- Connect to the input service to detect when the player fires the weapon
game:GetService("UserInputService").InputBegan:Connect(function(input, isTyping)
    if not isTyping and input.UserInputType == Enum.UserInputType.MouseButton1 then
        -- Calculate the direction of the raycast based on the camera's position and orientation
        local ray = camera:ViewportPointToRay(centerScreen.X, centerScreen.Y, Arm.Configuration:GetAttribute("Range"))

        -- Fire the raycast using FastCast
        FastCast:Fire(ray, Velocity, RaycastParams, RayCastBehavior)

        -- Play sound effects, create visual effects, etc.
        -- ...
    end
end)
3 Likes

Bruh ViewerportPointToRay okkkkk

2 Likes

ok😂. sorry if I sounded rude, I didn’t understand if it was intentional and did something wrong or if it was a typo

okay, it is a lot better now and its shooting around the center but it is landing on the crosshair point, I would ad an offset but the direction if different every time i change my camera orientation

wait I dont know give me 5 minutes

This?

local camera = game.Workspace.CurrentCamera
local viewportSize = camera.ViewportSize
local centerScreen = Vector2.new(viewportSize.X / 2, viewportSize.Y / 2)
local offset = Vector3.new(0, 0, 50) -- Adjust this to set the desired offset

-- Connect to the input service to detect when the player fires the weapon
game:GetService("UserInputService").InputBegan:Connect(function(input, isTyping)
    if not isTyping and input.UserInputType == Enum.UserInputType.MouseButton1 then
        -- Calculate the direction of the raycast based on the camera's position and orientation
        local ray = camera:ViewportPointToRay(centerScreen.X, centerScreen.Y, Arm.Configuration:GetAttribute("Range"))

        -- Calculate the final destination point for the bullet
        local destination = ray.Position + ray.Direction * Arm.Configuration:GetAttribute("Range") + offset

        -- Fire the raycast using FastCast
        FastCast:Fire(Ray.new(ray.Position, (destination - ray.Position).Unit * Arm.Configuration:GetAttribute("Range")), Velocity, RaycastParams, RayCastBehavior)

        -- Play sound effects, create visual effects, etc.
        -- ...
    end
end)

I hope this helps

3 Likes

This actually worked abit . Thanks, you’re a life saver! I just need to tweak a few things

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.