Keep tool in camera view?

Hello! I am making a ghost hunting game and I want the tool to stay with the camera rotation, for example, if I’m looking up I want the tool to be at the bottom right of the camera. (also it would be nice for the tool to look at the camera)

Here is a video demonstrating the issue:

I haven’t tried anything yet, because I couldn’t find anything. (If video doesn’t work right-click and click on “Open audio in New Tab”. It will download the video for you)

Thanks for any help!

like a viewmodel? If you want to create one of those (which are usually just a pair of fake arms used in FPS games), you can apply the viewmodel to the camera’s CFrame, like so:

-- Code like this are ran on the Client and are ran using RunService.RenderStepped

local Camera = workspace.CurrentCamera -- the Players Camera
local Offset = CFrame.new(0,0,0) -- an Offset from the Camera's CFrame

Model:PivotTo(Camera.CFrame * Offset) -- Pivots using the corresponding Data

Hhowever if you are going to move the Motor6D, it would ne more complicated.

I see what you mean but how can I attach the items to the viewmodel? Like right now, I made a part called “ViewmodelTest” and got it placed in front of the player:

People use a Weld known as a Motor6D which allows to Rig Items, and Animate them.

1 Like

What I did for my viewmodel system is I parented it to the…

Okay, too complicated. You’ll want to only use tools.

What you want to do is detect the tool grip (weld) in the RightHand part of the character. Once that’s done, you can modify the C0 yourself.

I’ve made this system right here to accomplish this:
ToolCamera.rbxl (48.3 KB)

1 Like

Thanks a lot! For me, all this “viewmodel” stuff is complicated. I’m an average programmer that likes roblox. Thanks!

I’ve found that viewmodels are much worth it for FPS games, but if you’re casually using something to work with any tool, this is the way to go.

1 Like

One question, the other players’ view of the item looks way messed up.
On the other screens the tool is like way up in the air or in the ground. How can I fix this?

It shouldn’t be. Try disabling the script and test out the tool normally, I think it’s a problem with your tool’s grip.

Without the script:


With the script:
image

Try this script instead:

local players = game:GetService("Players")
local runService = game:GetService("RunService")
local player = players.LocalPlayer

local currentGrip
local baseCF
local tool
local character
player.CharacterAdded:Connect(function(c)
	character = c
	local RightHand = character:WaitForChild('RightHand',1)
	assert(RightHand, 'Character failed to load.')
	RightHand.ChildAdded:Connect(function(child)
		if child:IsA("Weld") and child.Name == "RightGrip" then
			baseCF = child.C0
			local newGrip = child:Clone()
			newGrip.Parent = child
			--child.Enabled = false
			currentGrip = newGrip
		end
	end)
	character.ChildAdded:Connect(function(child)
		if child:IsA("Tool") then
			tool = child
		end
	end)
end)

function lerp(a, b, t)
	return a + (b - a) * t
end

local avgdt = 1/60

local offsetCF = CFrame.new(1, -1, -2)
local xz = Vector3.new(1,0,1)

local vel = 0

local bobCF1 = CFrame.identity
local bobCF2 = CFrame.identity

runService.RenderStepped:Connect(function(dt)
	if currentGrip then
		local vel2 = (character.PrimaryPart.AssemblyLinearVelocity*xz).Magnitude
		vel = lerp(vel, vel2, avgdt)
		local bobT = time()*(vel2/2)
		local bob2 = math.cos(bobT)*vel/60
		local bob1 = math.sin(bobT)*vel/360
		local bob3 = bob2*bob1
		bobCF1 = bobCF1:Lerp(CFrame.Angles(bob3, bob1, bob3),avgdt*10)
		bobCF2 = bobCF2:Lerp(CFrame.Angles(bob3, bob3, 0),avgdt*10)
		currentGrip.C0 = currentGrip.Part0.CFrame:ToObjectSpace(workspace.CurrentCamera.CFrame)-- * baseCF
			* bobCF1 * offsetCF * bobCF2
		avgdt = lerp(avgdt, dt, .01)
	end
end)

This one will clone the grip and the original one will be the only one that replicates.

1 Like

That script fixed the issue, thanks!

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