I am making an FPS game, and are testing the grounds with viewmodels. What I currently have, is an entire rig of a player, minus the legs, who’s PrimaryPartCFrame is set to the Camera’s CFrame on every RenderStepped. I followed EgoMoose’s tutorial to a t, got a working viewmodel, didn’t know where to go from there, nor did I understand a thing I read and decided to try something else that I would have an easier time with. I have a BasePart (transparent red block in the screenshot), which is welded to the gun. The gun is connected to the player’s hand via a Motor6D, and the viewmodel connected to the Camera via the RenderStepped function I mentioned at the start. How would I go about tweening or lerping the “Aim” part to be set to the Camera’s CFrame?
I don’t understand. Do you want to lerp the camera to the red part, like you’re aiming? Or do you want to lerp the red part to the camera, like that hands are repositioning?
Honestly, either or. I don’t know how to accomplish either, and they would both achieve the same effect.
There’s many ways to do this. A simple way (obviously not the best) would be to use the Heartbeat event and toggle the camera to focus and interpolate to the AimPart. The only reason you’d use a Heartbeat is to update the camera’s cframe with the most recent cframe. If the camera moves, the cframe changes too.
I tried to make this as simple as possible so that it’s easier to understand.
local userInputService = game:GetService("UserInputService")
local runService = game:GetService("RunService")
local Player = game.Players.LocalPlayer
local Character = Player.Character
local Camera = game.Workspace.CurrentCamera
local aimPart = game.Workspace:WaitForChild("AimPart")
function ignore()
-- Weld AimPart to RightHand
local weld = Instance.new("Weld")
weld.Parent = aimPart
weld.Part0 = Character:WaitForChild("RightHand")
weld.Part1 = aimPart
end
ignore() -- Ignore if "AimPart" is already welded to something
function aim(part)
Camera.CameraType = Enum.CameraType.Scriptable
Camera.CFrame = part.CFrame -- if you want this to be smooth, :Interpolate would work but you'd have to set the camera subject & focus (https://developer.roblox.com/en-us/api-reference/function/Camera/Interpolate)
end
runService.Heartbeat:Connect(function(step)
if userInputService:IsKeyDown(Enum.KeyCode.Q) then
return aim(aimPart) -- Contents of this could go directly here, but for the sake of learning, I separated it.
end
-- Resets the Player's Camera to default
Camera.CameraType = Enum.CameraType.Custom
Camera.CameraSubject = Character:FindFirstChild("Humanoid")
end)
