ViewModel swaying weirdly

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? I want the ViewModel to sway as players movement/camera

  2. What is the issue? It just doesnt even move no need to send ig

  3. What solutions have you tried so far? I tried searching up online but my small brain doesnt understand

4.here is the vid: Watch 2024-04-09 14-32-59 | Streamable

local RunService = game:GetService("RunService")

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Guns = ReplicatedStorage.Guns
--local ViewModels = ReplicatedStorage.ViewModels

local Players = game:GetService("Players")

local player = Players.LocalPlayer

local Camera = game.Workspace.CurrentCamera

local UIS = game:GetService("UserInputService")

local module = {}

local gunConnection
local isWalking
local isAiming = false

local function PositionViewModel(gun: Model, ViewModel, config)
	local gunOffset = config.Offset
	local lastCameraCFrame = Camera.CFrame
	local targetCFrame = CFrame.new()
	local swayOffset = CFrame.new()
	local swaySize = 1 

	gun.Parent = ViewModel
	gun:PivotTo(ViewModel.R.GripPos.CFrame * gunOffset)

	gunConnection = RunService.RenderStepped:Connect(function(deltaTime)
		local Character = player.Character or player.CharacterAdded:Wait() 
		if Character  then
			local humanoid = Character:FindFirstChild("Humanoid")
			if humanoid.MoveDirection.Magnitude > 0 then
				isWalking = true
			else
				isWalking = false
			end

			local aimOffset = gun.Aim.CFrame:Inverse() * Camera.CFrame * CFrame.new(0,-0.7, 0)
			local walkSineWave = math.sin(time() * 2*4) / 25
			local walkCosWave = math.sin(time() *3*4) / 20
			local walkSineRotation = math.sin(time() * 2*4) / 25
			local walkCosRotation = math.sin(time() * 3*4) / 20


			local X,Y,Z = Camera.CFrame:ToObjectSpace(lastCameraCFrame):ToOrientation()
			swayOffset = swayOffset:Lerp(CFrame.Angles(math.sin(X / 2)* swaySize, math.sin(Y / 2) * swaySize, 0), 0.1)	

			local walkCFrameOffset = CFrame.new(walkSineWave, walkCosWave, 0) * CFrame.fromEulerAnglesXYZ(0, 0, walkSineRotation)

			if isAiming then
				walkCFrameOffset = CFrame.new(walkSineWave/10, walkCosWave/10, 0)
			end

			if isWalking and not isAiming then
				targetCFrame = targetCFrame:Lerp(gunOffset * walkCFrameOffset * swayOffset, 0.1)
			elseif not isWalking and not isAiming then
				targetCFrame = targetCFrame:Lerp(gunOffset * swayOffset * swayOffset, 0.1)
			end

			if isWalking and isAiming then
				targetCFrame = targetCFrame:Lerp(walkCFrameOffset * swayOffset * aimOffset, 0.1)
			elseif not isWalking and isAiming then
				targetCFrame = targetCFrame:Lerp(swayOffset * aimOffset, 0.1)
			end

			lastCameraCFrame = Camera.CFrame
			ViewModel:PivotTo(ViewModel.R.GripPos.CFrame * targetCFrame)

--[[gun sways when I change the line to this gun:PivotTo(ViewModel.R.GripPos.CFrame * targetCFrame) but viewModel doesnt]]
		

		end
	end)

end

function module.EquipGun(gunName: string, ViewModel: string)
	local foundGun = Guns:FindFirstChild(gunName)

	if foundGun == nil then
		return
	end
	local gun = foundGun:Clone()
	local VM = Camera:FindFirstChild(ViewModel)

	PositionViewModel(gun, VM, require(foundGun.Config))
end



return module

Please explain it like you are explaining to a monkey

2 Likes

Is HumanoidRootPart camera for your viewmodel?
If no, then try change it

2 Likes

İt is HumanoidRootPart but the position changes so weirdly

1 Like
local function PositionViewModel(gun: Model, ViewModel, config)
	local gunOffset = config.Offset
	local lastCameraCFrame = Camera.CFrame
	local targetCFrame = CFrame.new()
	local swayOffset = CFrame.new()
	local swaySize = 0.5

	gun.Parent = ViewModel
	gun:PivotTo(ViewModel.R.GripPos.CFrame * gunOffset)

	gunConnection = RunService.RenderStepped:Connect(function(deltaTime)
		local Character = player.Character or player.CharacterAdded:Wait() 
		if Character  then
			local humanoid = Character:FindFirstChild("Humanoid")
			if humanoid.MoveDirection.Magnitude > 0 then
				isWalking = true
			else
				isWalking = false
			end

			local aimOffset = gun.Aim.CFrame:Inverse() * Camera.CFrame * CFrame.new(0,-0.7, 0)
			local walkSineWave = math.sin(time() * 2*4) / 20
			local walkCosWave = math.sin(time() *3*4) / 15
			local walkSineRotation = math.sin(time() * 2*4) / 20
			local walkCosRotation = math.sin(time() * 3*4) / 15


			local X,Y,Z = Camera.CFrame:ToObjectSpace(lastCameraCFrame):ToOrientation()
			swayOffset = swayOffset:Lerp(CFrame.Angles(math.sin(X / 2)* swaySize, math.sin(Y / 2) * swaySize, 0), 0.1)	

			local walkCFrameOffset = CFrame.new(walkSineWave, walkCosWave, 0) * CFrame.fromEulerAnglesXYZ(0, 0, walkSineRotation)

			if isAiming then
				walkCFrameOffset = CFrame.new(walkSineWave/10, walkCosWave/10, 0)
			end

			if isWalking and not isAiming then
				targetCFrame = targetCFrame:Lerp(gunOffset * walkCFrameOffset * swayOffset, 0.1)
			elseif not isWalking and not isAiming then
				targetCFrame = targetCFrame:Lerp(gunOffset * swayOffset * swayOffset, 0.1)
			end

			if isWalking and isAiming then
				targetCFrame = targetCFrame:Lerp(walkCFrameOffset * swayOffset * aimOffset, 0.1)
			elseif not isWalking and isAiming then
				targetCFrame = targetCFrame:Lerp(swayOffset * aimOffset, 0.1)
			end

			lastCameraCFrame = Camera.CFrame
			
			ViewModel.HumanoidRootPart:PivotTo(ViewModel.HumanoidRootPart.CFrame * targetCFrame)				

		end
	end)

end

I just fixed it but now the movement is really scrambled. here is the vid Watch 2024-04-09-20-37-45 | Streamable

1 Like

can anyone help Im still stuck on this

1 Like

can anyone help Im still stuck on this

1 Like

can anyone help Im still stuck on this

1 Like

can anyone help Im still stuck on this