How do i make my character's gun follow the mouse's position?

the title explains it, i dont mean i want the characters arm to follow the mouse’s position, i mean the entire character should rotate, but with how it currently works, the character faces the mouse’s position and not the gun, how can i fix this?

how i want it to look:
image

how it currently works:
image



the current script:

local RunService = game:GetService("RunService")

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local HumanoidRP = Character:WaitForChild("HumanoidRootPart")

local Mouse = Player:GetMouse()
local Gun = nil

function StartRender()
	TurnPlayer = RunService.RenderStepped:Connect(function()
		local MousePosition = Vector3.new(
			Mouse.Hit.Position.X,
			HumanoidRP.Position.Y,
			Mouse.Hit.Position.Z
		)

		local HRPPosition = HumanoidRP.Position

		HumanoidRP.CFrame = CFrame.new(HRPPosition, MousePosition)
	end)
end


Character.ChildAdded:Connect(function(Item)
	if Item:HasTag("Gun") then
		Gun = Item
		StartRender()
	end
end)


Character.ChildRemoved:Connect(function(Item)
	if Item == Gun then
		if TurnPlayer.Connected == true then
			TurnPlayer:Disconnect()
		end
		Gun = nil
	end
end)
4 Likes

Hi, can you show me the script that adjusts the character’s position on the mouse?

2 Likes

You’ll want to apply an extra offset to where the character is facing based off the offset between the character and the gun’s barrel

1 Like

i added the script to the post

1 Like

it works at a distance but when the mouse gets close the character starts to go crazy

new script:

local RunService = game:GetService("RunService")

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local HumanoidRP = Character:WaitForChild("HumanoidRootPart")

local Mouse = Player:GetMouse()
local Gun = nil

function StartRender()
	TurnPlayer = RunService.RenderStepped:Connect(function()
		local Offset = (Gun.Handle.Position - HumanoidRP.Position)
		
		local MousePosition = Vector3.new(
			Mouse.Hit.Position.X,
			HumanoidRP.Position.Y,
			Mouse.Hit.Position.Z
		) - Vector3.new(Offset.X, 0, Offset.Z)
		
		local HRPPosition = HumanoidRP.Position
		HumanoidRP.CFrame = CFrame.new(HRPPosition, MousePosition)
	end)
end


Character.ChildAdded:Connect(function(Item)
	if Item:HasTag("Gun") then
		Gun = Item
		StartRender()
	end
end)


Character.ChildRemoved:Connect(function(Item)
	if Item == Gun then
		if TurnPlayer.Connected == true then
			TurnPlayer:Disconnect()
		end
		Gun = nil
	end
end)

I tried your code in my studio (without the code to block the camera on top) And I didn’t shake like in your video.

I figured out a solution, you can disable character Rotation if the cursor is too close to you

Code:

local RunService = game:GetService("RunService")

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local HumanoidRP = Character:WaitForChild("HumanoidRootPart")

local Mouse = Player:GetMouse()
local Gun = nil

function StartRender()
	TurnPlayer = RunService.RenderStepped:Connect(function()
		local Offset = (Gun.Handle.Position - HumanoidRP.Position)

		local MousePosition = Vector3.new(
			Mouse.Hit.Position.X,
			HumanoidRP.Position.Y,
			Mouse.Hit.Position.Z
		) - Vector3.new(Offset.X, 0, Offset.Z)
		
		local distance = (HumanoidRP.Position - MousePosition).Magnitude
		local MaxDistance = 2
		
		if distance > MaxDistance then
			local HRPPosition = HumanoidRP.Position
			HumanoidRP.CFrame = CFrame.new(HRPPosition, MousePosition)		
		else
			-- Rotation stopped!
		end
	
	end)
end

well, it kind of works, but id prefer it to continue rotating the player at any distance, also when you say “without the code to block the camera on top”, if you meant without the camera being locked above the character looking down, i just tried it without it being locked and it still makes the player go crazy

I think the shaking has to do with what you did to your gun.
Since I didn’t have the shake without it, i think you something to do about it.

I think another way is needed that doesn’t involve offset.
Or some way to fix it with what is already there