You can write your topic however you want, but you need to answer these questions:
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
What is the issue? Include screenshots / videos if possible!
The bullet is not centerd
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)
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)
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)
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
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)