Hello guys.

I'm trying to make some first person animations and I wanted to make everything seem a bit more realistic by adding a bit of model sway. The problem is that once I added the sway, the viewmodel doesn't stick to the camera anymore. Here's what I mean.

Here's the local script:

local player = game:GetService("Players").LocalPlayer;
local run = game:GetService("RunService");
local camera = workspace.CurrentCamera;
local char = game:GetService("Players").LocalPlayer.Character or game:GetService("Players").LocalPlayer.CharacterAdded:wait();
local viewModel = game.ReplicatedStorage:WaitForChild("viewModel"):Clone();

local s = require(game.ReplicatedStorage.Sett.Settings); -- only has CFrame for the viewmodel, set to 0, -1.7, -0.55

player.CameraMode = Enum.CameraMode.LockFirstPerson;

viewModel:SetPrimaryPartCFrame(camera.CFrame);
viewModel.Parent = camera;

local idle = viewModel.Humanoid:LoadAnimation(game.ReplicatedStorage.Sett.Settings.Animations.Idle);
local cover = viewModel.Humanoid:LoadAnimation(game.ReplicatedStorage.Sett.Settings.Animations.Cover);

run.RenderStepped:connect(function()
	if char.Humanoid.MoveDirection.Magnitude > 0 then
	idle:Play();
	end
end);


local springModule = require(game.ReplicatedStorage.Sett.Spring);
local spring = springModule.new(viewModel.PrimaryPart.CFrame.p, Vector3.new(), camera.CFrame.p);

spring.k = 0.05;
spring.friction = 0.1;

run.RenderStepped:connect(function()
	spring.target = camera.CFrame.p;
	spring:update();
	--viewModel:SetPrimaryPartCFrame(camera.CFrame * s.handpos);
	viewModel.HumanoidRootPart.CFrame = CFrame.new(spring.position)* camera.CFrame;
end)

And here's the module:

local spring = {};

function spring.new(position, velocity, target)
	local self = setmetatable({}, {__index = spring});
	
	self.position = position;
	self.velocity = velocity;
	self.target = target;
	self.k = 1;
	self.friction = 1;
	
	return self;
end;

function spring:update()
	local d = (self.target - self.position);
	local f = (d * self.k);
	self.velocity = (self.velocity * (1- self.friction)) + f;
	self.position = self.position + self.velocity;
	
end;


return spring;

I'm guessing the problem is here,

viewModel.HumanoidRootPart.CFrame = CFrame.new(spring.position)* camera.CFrame;

But I'm not exactly sure that is wrong or how to fix it. Any help?

Thanks, Truta

Edit: forgot to remove a line

1 comment

0

Was this ever solved?