Third Person Shooter Logic

I’m currently making a third person game where some characters can shoot, and in most situations it works fine! However since it is third person, it’s possible that your camera can be aiming at something that is behind you. As you can see in the image, it will shoot backwards at the brick.

Here is the code that handles shooting:

function shootM1()
	local currentCamera = workspace.CurrentCamera
	local VIEWPORT_SIZE = currentCamera.ViewportSize
	local unitRay = currentCamera:ViewportPointToRay(VIEWPORT_SIZE.X / 2, VIEWPORT_SIZE.Y / 2)
	local originPoint = originPoint.Position
	local myParams = RaycastParams.new()
	myParams.FilterType = Enum.RaycastFilterType.Exclude
	myParams.FilterDescendantsInstances = {myCharacter}
	local hitscan = workspace:Raycast(unitRay.Origin, unitRay.Direction*1000, myParams)
	local focalPoint = if hitscan then hitscan.Position else unitRay.Origin+unitRay.Direction*1000
	local direction = CFrame.lookAt(originPoint, focalPoint).LookVector
	local extraArgs = {direction, originPoint} --n.
	weapon.AbilityHandler:FireServer("m1", extraArgs)
end

I’m more so just looking for advice on the best way to prevent this, and the best solution on what to do when this happens!

1 Like

I need help with this too. Let me know if you found a solution.

maybe check the LookVector of the hrp and limit the direction of the raycast?

Not sure about limiting its angle, but you could prevent the shooting when the dot product of the ray direction and the lookvector of the humanoidRootPart is greater than a certain amount:

local Dot = direction:Dot(Root.CFrame.LookVector)
if dot < 0 then return end

weapon.AbilityHandler:FireServer("m1", extraArgs)

I didnt define Root, im assuming you can do that.

1 Like

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