I’m trying to create an Aim Offsets system where when the player looks up and down their character bends their waist and arms with the camera movement. My game is a third person shooter and the camera is already fixed behind the player with the mouse moving on the X axis rotating the character to the left and right. I just need the mouse moving on the Y axis to make the player look up and down.
I've tried doing it with an animation like this but the character shakes like he's having a seizure.
Vid:
Imgur: The magic of the Internet
LocalScript in StarterCharacterScripts:
local PlayerService = game:GetService("Players")
local RunService = game:GetService("RunService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ContextActionService = game:GetService("ContextActionService")
local CollectionService = game:GetService("CollectionService")
local localPlayer = PlayerService.LocalPlayer
local character = localPlayer.Character or localPlayer.Character:wait()
local humanoid = character:WaitForChild("Humanoid")
local mouse = localPlayer:GetMouse()
-- Setup
local gunFolder = ReplicatedStorage:WaitForChild("Guns")
local aimAnim = gunFolder:WaitForChild("PistolOffsets")
local animTrack = humanoid:LoadAnimation(aimAnim)
animTrack:AdjustSpeed(0)
animTrack:Play(0)
local maxRot = math.rad(45)
local maxMove = 3
local smooth = 0.05
local idealRotation
local function PlayerHoldingGun()
local characterParts = localPlayer.Character:GetChildren()
local found = false
for i,part in ipairs(characterParts) do
if CollectionService:HasTag(part, "Gun") then -- and part:GetAttribute("Zoom") ~= nil
found = true
end
end
return found
end
local function Adjust()
if not PlayerHoldingGun() then
animTrack:Stop()
return
end
if animTrack == nil then
local animTrack = humanoid:LoadAnimation(aimAnim)
animTrack:AdjustSpeed(0)
animTrack:Play()
end
local cam = workspace.CurrentCamera
local cameraCFrame = cam.CFrame
local currentOrientationX = cameraCFrame.LookVector.Y
local difference = currentOrientationX
local animPos = (difference + 1) / 2
if not animTrack.IsPlaying then
animTrack:Play(0)
end
-- Still unsure if this might help with shaking
--animTrack.Priority = Enum.AnimationPriority.Action4
local currentPos = animTrack.Length * animPos
animTrack.TimePosition = currentPos
end
RunService:BindToRenderStep("AdjustAimOffsetsAndCamera", Enum.RenderPriority.Last.Value, Adjust)
And I tried using the Rig like this but it doesn't work well with the animations. It doesn't work on the left arm and when I play the firing animation it moves down if the player is looking up.
Vid:
Imgur: The magic of the Internet
Client:
local ReplicatedStorage = game:WaitForChild("ReplicatedStorage")
local PlayerService = game:GetService("Players")
local CollectionService = game:GetService("CollectionService")
local localPlayer = PlayerService.LocalPlayer
local remoteEvents = ReplicatedStorage:WaitForChild("RemoteEvents");
local camera = workspace.CurrentCamera
local function PlayerHoldingGun()
local characterParts = localPlayer.Character:GetChildren()
local found = false
for i,part in ipairs(characterParts) do
if CollectionService:HasTag(part, "Gun") then -- and part:GetAttribute("Zoom") ~= nil
found = true
end
end
return found
end
local function UpdateRotation()
remoteEvents.tiltAt:FireServer(math.asin(camera.CFrame.LookVector.y));
end
game["Run Service"].RenderStepped:Connect(function(deltaTime)
if PlayerHoldingGun() then
UpdateRotation()
end
end)
Server:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvents = ReplicatedStorage:WaitForChild("RemoteEvents")
local tiltEvent = remoteEvents:WaitForChild("tiltAt")
local neckC0 = CFrame.new(0, 0.9, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1)
local waistC0 = CFrame.new(0, 0.2, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1)
local rShoulderC0 = CFrame.new( 1, 0.5, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1)
local lShoulderC0 = CFrame.new(-1, 0.5, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1)
tiltEvent.OnServerEvent:Connect(function(player, theta)
local character = player.Character or player.CharacterAdded:Wait()
local neck = character:WaitForChild("Head"):WaitForChild("Neck")
local waist = character:WaitForChild("UpperTorso"):WaitForChild("Waist")
local rShoulder = character:WaitForChild("RightUpperArm"):WaitForChild("RightShoulder")
local lShoulder = character:WaitForChild("LeftUpperArm"):WaitForChild("LeftShoulder")
neck.C0 = neckC0 * CFrame.fromEulerAnglesYXZ(theta*0.5, 0, 0)
waist.C0 = waistC0 * CFrame.fromEulerAnglesYXZ(theta*0.5, 0, 0)
rShoulder.C0 = rShoulderC0 * CFrame.fromEulerAnglesYXZ(theta*1.5, 0, 0)
lShoulder.C0 = lShoulderC0 * CFrame.fromEulerAnglesYXZ(theta*1.5, 0, 0)
end)
Does anyone know how I can fix these scripts or something else I can do to get this effect?