How could I make my viewmodel face forward towards the camera?

I’m learning how to create a viewmodel and understand the idea, but the issue is that it doesn’t face forwards due to the CFrame. Adjusting the orientation causes glitches as well.

Changing orientation outside of CFrame causes glitches as seen in the video. I don’t know of any alternatives, should I avoid using the camera?

LocalScript inside StarterGUI
local viewmodel = game:GetService("ReplicatedStorage"):WaitForChild("Viewmodel"):Clone()
viewmodel.Parent = workspace

local viewmodelparts = viewmodel:GetChildren()

local camera = workspace.CurrentCamera
local camcframe = camera.CFrame
local plr = game:GetService("Players").LocalPlayer

local event = game:GetService("ReplicatedStorage"):WaitForChild("viewmodel_event")

camera:GetPropertyChangedSignal("CFrame"):Connect(function() -- detect when the camera moves
	if camera.CFrame ~= camcframe then
		game:GetService("TweenService"):Create(viewmodel.PrimaryPart, TweenInfo.new(0.1, Enum.EasingStyle.Sine), { CFrame = camcframe + Vector3.new(0, -1.4, 0) }):Play() -- lag viewmodel behind
		game:GetService("TweenService"):Create(viewmodel.PrimaryPart, TweenInfo.new(0.1, Enum.EasingStyle.Sine), { Orientation = Vector3.new(camera.Focus) }):Play()
		camcframe = camera.CFrame -- set camcframe to new cframe
	else
		return -- if it didn't move return
	end
end)

event.OnClientEvent:Connect(function(arm, action) -- viewmodel animation event
	local selected = viewmodel:WaitForChild(arm.."Arm"):GetChildren() -- find the select arm
	
	for i, v in pairs(selected) do -- make selected arm visible
		if v:IsA("MeshPart") then
			v.Transparency = 0
		end
	end
	
	if action == "item" then -- check what the action is (item is only action)
		local animator = viewmodel:WaitForChild("Humanoid"):WaitForChild("Animator")

		local grabanim = Instance.new("Animation")
		grabanim.AnimationId = "rbxassetid://12602387442"

		local grabtrack = animator:LoadAnimation(grabanim)
		grabtrack:Play()
		
		print("playing anim")
		-- finish this part later
	else
		return -- if it's none of the actions return
	end
end)
1 Like

maybe try:
image

2 Likes

What is the item that is bouncing around supposed to be doing, and is it supposed to be held by the player?
If you’ve got it at the Camera location and are trying to get it to point toward the Camera I can see why it’s spinning around that way. It is basically trying to point to itself.

It’s the viewmodel and it’s position is set to the camera’s CFrame, I’m trying to make it point towards the camera’s focus point.

I’m using the camera’s CFrame so I don’t think I need to make a new one, but if I do can you explain.

edit: alright I guess I just didn’t understand what you meant at first but this does work, thanks a lot!

Please copy/paste your script so others can see exactly how you are doing it.
Put 3 backticks before and after your script so it formats properly.

1 Like

Alright, I edited my post to show the script.

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