Character facing towards mouse — Snaps when hovering mouse over part

Beginner scripter here, trying to recreate InFamous aiming and shooting system. So far I’ve gotten a functional camera/aiming system, along with the character facing towards the mouse, as it should in a third-person-shooter, though I have found a small issue that could compromise the shooting system in the future; hovering the mouse over a nearby obstacle snaps the character to face towards it.

(InFamous’ aiming and shooting for reference)
I believe this issue might be caused mostly by the camera’s horizontal offset, which makes the character’s rotation more noticeable. The offset turns the ray from the character to mouse.Hit.Position into a diagonal, which seems to slightly shift the character in order to properly force towards the mouse.Hit.Position. I’m pretty sure that’s the cause for this issue, but still, I have no idea on how to fix it, so any help is appreciated.

Below are the scripts that handle the camera and character’s rotation:

“PlayerInput” (LocalScript within StarterCharacterScripts)

local player = game:GetService("Players").LocalPlayer
local character = player.Character
local humanoid = character:WaitForChild("Humanoid")

local camera = workspace.CurrentCamera
local aimingOffset = Vector3.new(2, .7, 0)
local defaultCameraOffset = Vector3.new(0,1,0)
local defaultFOV = 70


local UserInputService = game:GetService("UserInputService")
local isRightMouseButtonHeld = false
local boltEvent = game.ReplicatedStorage.Events.LightningBolt
local stateModule = require(game.ReplicatedStorage.Module.stateModule)
local isAiming = stateModule.isAiming 

UserInputService.MouseIconEnabled = false
local debounce = false

local function onInputBegan(input, isProcessing)
	if not isProcessing and input.UserInputType == Enum.UserInputType.MouseButton2 then
		isRightMouseButtonHeld = true
		isAiming = true
		if isAiming then
			UserInputService.MouseIconEnabled = true
			print("Aiming is " .. tostring(isAiming))
			camera.FieldOfView = 25
			humanoid.CameraOffset = aimingOffset
		end
	end
end

local function onInputEnded(input, isProcessing)
	if not isProcessing and input.UserInputType == Enum.UserInputType.MouseButton2 then
		isRightMouseButtonHeld = false
		isAiming = false
		if not isAiming then
			UserInputService.MouseIconEnabled = false
			print("Aiming is " .. tostring(isAiming))
			camera.FieldOfView = defaultFOV	
			humanoid.CameraOffset = defaultCameraOffset
		end
	end
end


local function shootProjectile(input)
	if isAiming and input.UserInputType == Enum.UserInputType.MouseButton1 then
		boltEvent:FireServer()
	end
end

UserInputService.InputBegan:Connect(onInputBegan)
UserInputService.InputBegan:Connect(shootProjectile)
UserInputService.InputEnded:Connect(onInputEnded)

“CameraBehavior” (LocalScript within StarterPack)

local Players = game:GetService("Players")
local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local humanoidRootPart = humanoid.RootPart
local camera = workspace.CurrentCamera
local mouse = player:GetMouse()

local StarterPlayer = game:GetService("StarterPlayer")
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
	
local StarterPlayer = game:GetService("StarterPlayer")
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")


RunService.Heartbeat:Connect(function()
	local mousePos = mouse.Hit.Position
	local targetPosition = Vector3.new(mousePos.X, humanoidRootPart.Position.Y, mousePos.Z)

	UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
	humanoidRootPart.CFrame = CFrame.new(humanoidRootPart.Position, targetPosition)

end)

UserInputService.MouseIcon = "rbxassetid://15860340814"
player.CameraMaxZoomDistance = 13
player.CameraMinZoomDistance = 13

Once again, I am a beginner when it comes to scripting, learning by myself using only the documentation and forum, very rarely relying on tutorials (as most are just copy & pasting anyways), so any other advice regarding how I’m approaching these systems are more than welcome. I’m pretty sure I’m doing everything in the most horrid, painful, impractical way. Thank you in advance!

1 Like

Fixed it, I guess?

Setting mouse.TargetFilter = workspace made mouse.Hit ignore every single part within the workspace, which no longer causes that stutter/snapping due to the mouse hitting parts, though it feels mostly like a band-aid fix.

I’ll keep the post open for a while, since I’m not confident with this solution and still open to suggestions and/or advice.