-
What do you want to achieve? Keep it simple and clear!
I’m using Quenty’s springs to make viewmodel weapon sway. I want to use my idle and reload animation whilst the weapon is swaying. -
What is the issue? Include screenshots / videos if possible!
The animations for the arms of my viewmodel are broken.
Here’s what it looks like (With Sway)
-
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I’ve tried messing with the viewmodel’s properties and messing with the Motor6Ds connected to the arms.
What I want it to look like (Without sway)
local rightArmPart = viewModel.RightUpperArm
local leftArmPart = viewModel.LeftUpperArm
local rightCameraOffset = RightArmJointC0CFrame -- CFrame.Angles(math.rad(90), 0, 0) * CFrame.new(0.75, 0, .75)
local leftCameraOffset = LeftArmJointC0CFrame -- CFrame.Angles(math.rad(90), 0, 0) * CFrame.new(-0.75, 0, .75)
local camera = workspace.CurrentCamera
local RunService = game:GetService("RunService")
local Spring = require(ReplicatedStorage.CombatSys.Directories.SwayModule)
local ZEROVECTOR =Vector3.new()
local rightArmViewmodelSpring = Spring.new(ZEROVECTOR)
rightArmViewmodelSpring.Speed = 10
rightArmViewmodelSpring.Damper = 1
local leftArmViewmodelSpring = Spring.new(ZEROVECTOR)
leftArmViewmodelSpring.Speed = 10
leftArmViewmodelSpring.Damper = 1
local function clampMagnitude(vector, maxMagnitude)
return (vector.Magnitude > maxMagnitude and (vector.Unit * maxMagnitude) or vector)
end
function angleBetween(vector1, vector2)
return math.acos(math.clamp(vector1.Unit:Dot(vector2.Unit), -1, 1))
end
local deltaSensitivity = -2 -- increases force from mouse delta
--if negative force goes in opposite direction, viewmodel is lagging behind
local maxAngle = 30
local rightArmPreviousGoalCFrame = CFrame.new()
local lefttArmPreviousGoalCFrame = CFrame.new()
local function springUpRight()
local goalCFrame = camera.CFrame*rightCameraOffset
rightArmPart.CFrame = goalCFrame
--Spring stuff
local differenceCF = rightArmPreviousGoalCFrame:ToObjectSpace(goalCFrame)
local axis, angle = differenceCF:ToAxisAngle()
local angularDisplacement = axis*angle
rightArmPreviousGoalCFrame = goalCFrame
local springForce = angularDisplacement*deltaSensitivity
rightArmViewmodelSpring:Impulse(springForce)
local partSpringOffset = rightArmViewmodelSpring.Position
local axis = partSpringOffset.Unit
local angle = partSpringOffset.Magnitude
--clamp the angle don't want it to speen 360 degrees unless you want it to
--velocity goes wild though
angle = math.deg(angle)
if angle > maxAngle then
--print("Clamped")
--local maxAngularDisplacement = axis*angle
local currentViewModelVelocity = rightArmViewmodelSpring.Velocity
local collision = math.sign(currentViewModelVelocity:Dot(axis))
--1 is colliding, -1 is going away from colliding wall normal
if collision > 0 then
local reactionAngle = angleBetween(currentViewModelVelocity.Unit,axis)
local resolve = math.cos(reactionAngle)
local reactionForce = -axis*currentViewModelVelocity.Magnitude*resolve
rightArmViewmodelSpring:Impulse(reactionForce)
end
end
angle = math.clamp(angle,0,maxAngle)
angle = math.rad(angle)
if angle > 0.001 then--Nan check checking if there is no spring caused rotation
rightArmPart.CFrame *= CFrame.fromAxisAngle(axis,angle)
end
end
RunService.RenderStepped:Connect(function(step)
springUpRight()
springUpLeft() -- (Just a copy of right)
end)