Theres a custom shiftlock in my game, so I want the raycast to start from where the camera/character is looking, but go towards the middle of the screen (crosshair) and where the character is looking. This is to prevent anything from being hit behind the character, but infront of the camera, I’ll show you an example:
For the beam effect that does effect the raycast, I will. But for the raycast I was thinking about it, but couldn’t it cause glitching and bugs? For example what if the animations glitched and didn’t play, the gun would be pointing down at the ground. That could effect the raycast.
If I direct it to the mouseposition (locked to the middle), wouldn’t it cause the problem of shooting stuff infront of the camera, but behind the player.
Then you fix those bugs that cause that. This is a thing even AAA games do.
Helldivers for example, with some guns if you emote and stick you gun up in the air but shoot, the bullets will go from barrel to mouse position. Essentially coming out the side of the barrel. It’s just not a big deal overall and as long as you handle the bugs when they come. Bugs that mess up your characters animation shouldn’t be so common anyways.
What i’ve done for my gun system im currently doing, heres some of what i did for the raycasting
-- Get the HumanoidRootPart for bullet origin reference
local root = character:FindFirstChild("HumanoidRootPart")
if not root then return end
-- Get the screen center ray
local screenCenter = Vector2.new(Camera.ViewportSize.X / 2, Camera.ViewportSize.Y / 2)
local rayFromCamera = Camera:ViewportPointToRay(screenCenter.X, screenCenter.Y)
-- Define new ray origin and direction
local rayOrigin = rayFromCamera.Origin
local rayDirection = rayFromCamera.Direction * gunData.bulletDistance
-- Set up raycast parameters
local rayParams = RaycastParams.new()
rayParams.FilterDescendantsInstances = {character} -- Ignore shooter
rayParams.FilterType = Enum.RaycastFilterType.Exclude
-- Perform raycast from the center of the screen
local rayResult = workspace:Raycast(rayOrigin, rayDirection, rayParams)
if rayResult then
local hitPart = rayResult.Instance
local hitCharacter = hitPart.Parent
local hitPlayer = game.Players:GetPlayerFromCharacter(hitCharacter)
if gunHit then
gunHit:FireServer(gunName, hitCharacter)
else
warn("GunHitEvent is missing in ReplicatedStorage")
end
end
local bulletOrigin = root.Position
local bulletDirection = rayDirection
-- Fire Bullet Effect
GunModule.BulletEffect(gunData.bulletType, bulletOrigin, bulletDirection)
-- Fire server-side event (for sound/visuals, not damage)
gunFire:FireServer(gunName, bulletOrigin, bulletDirection)
```