Help with Third Person Raycasting

I’m working on a TPS (third person shooter), but Im having problems with my raycasting so I’ll start over. Heres how I want it to work.

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:

Please let me know if you can help me, as I’d really appreciate it.

Wouldn’t it be better to start the raycast from the tip of the gun and mark its direction as the mouse position / crosshair?

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.

Well then maybe you can start it at the currentcamera position and direct it to the mouse position?

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.

1 Like

Thought there would be a better way, but thanks.

1 Like

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)
```

There probably is I’m just saying it’s at least not the end of the world to have have the temporary solution while you find a better one on the side.

Tried this way, works better, thanks!