Hello developers! Im writing an fps framework and since I like Games Unite and its smooth and polished viewmodel movement, I decided to design my viewmodel movement just like theirs.
I have posted about this before and found an answer for the sway, but after adapting it to a spring module, some issues have appeared and Ive also realized that it doesnt look much like Games Unite.
I can share code if needed, but I mostly want something that looks like games unite and is really smooth.
I’m sure they used a module script for the spring effect which goes like this:
-- Constants
local ITERATIONS = 8
-- Module
local SPRING = {}
-- Functions
function SPRING.create(mass, force, damping, speed)
local spring = {}
local self = setmetatable({}, spring)
self.Target = Vector3.new();
self.Position = Vector3.new();
self.Velocity = Vector3.new();
self.Mass = mass or 5;
self.Force = force or 50;
self.Damping = damping or 4;
self.Speed = speed or 4;
function spring.shove(force)
local x, y, z = force.X, force.Y, force.Z
if x ~= x or x == math.huge or x == -math.huge then
x = 0
end
if y ~= y or y == math.huge or y == -math.huge then
y = 0
end
if z ~= z or z == math.huge or z == -math.huge then
z = 0
end
self.Velocity = self.Velocity + Vector3.new(x, y, z)
end
function spring.update(dt)
local scaledDeltaTime = math.min(dt or 1/60, 1) * self.Speed / ITERATIONS
for i = 1, ITERATIONS do
local iterationForce = self.Target - self.Position
local acceleration = (iterationForce * self.Force) / self.Mass
acceleration = acceleration - self.Velocity * self.Damping
self.Velocity = self.Velocity + acceleration * scaledDeltaTime
self.Position = self.Position + self.Velocity * scaledDeltaTime
end
return self.Position
end
return spring
end
-- Return
return SPRING
The swaying mechanic is in a youtube video in the fps framework playlist by XERA dev, although I forgot the episode.
Hey, I don’t know if you’re still working on this project, but I found out how to make the sway pivot around the handle, here it is.
local function GetOffsetFromHandle(Rotation : Vector3)
local HandleOffset = Framework.Settings.ViewmodelPhysics.HandleOffset
return -(CFrame.new(HandleOffset) * CFrame.Angles(Rotation.X, Rotation.Y, Rotation.Z) * CFrame.new(-HandleOffset)).Position
end
You just have to create a seperate spring for the position offset and get the offset by the springs updated position, although, I am not sure if it will work the same for your project.
I just wanted to clarify something. It’s not that I don’t understand why people are facing issues with the game, because I absolutely do. I’m aware of the challenges that are present, and I think it’s important to acknowledge them so we can work towards solutions together.