I am using Quenty’s spring module for my weapon sway spring system. The issues I am having are, number one I cannot play animations over the weapon because I am setting the parts CFrame I believe, number two whenever I walk with my weapon and look around my player starts moving around in strange ways, number three When the player is not moving their mouse for a few seconds the weapon and arms disappear. Please help me understand why these issues are occuring thanks.
The two biggest issues I am having is the animation not playing on the part that I am applying the spring to and the biggest issue is that even when I set cancollide false the weapon is still able to push the player around when the player looks down and around.
Local Script in tool setting the parts CFrame:
local cameraOffset = CFrame.new(0.8, -0.75, -2.45)
local camera = CamXE
local RunService = game:GetService("RunService")
local Spring = require(script:WaitForChild("Spring"))
local ZEROVECTOR =Vector3.new()
local viewmodelSpring = Spring.new(ZEROVECTOR)
viewmodelSpring.Speed = 4
viewmodelSpring.Damper = 5 --1 is perfect dampening
local function clampMagnitude(vector, maxMagnitude)
return (vector.Magnitude > maxMagnitude and (vector.Unit * maxMagnitude) or vector)
end
local deltaSensitivity = 0.5 -- increases force from mouse delta
local previousGoalCFrame = CFrame.new()
RunService.RenderStepped:Connect(function()
if not LookGP then
return
end
local goalCFrame = camera.CFrame*cameraOffset
part.CFrame = goalCFrame
--Spring stuff
local differenceCF = previousGoalCFrame:ToObjectSpace(goalCFrame)
local axis, angle = differenceCF:ToAxisAngle()
local angularDisplacement = axis*angle
previousGoalCFrame = goalCFrame
local springForce = angularDisplacement*deltaSensitivity
viewmodelSpring:Impulse(springForce)
local partSpringOffset = viewmodelSpring.Position
local axis = partSpringOffset.Unit
local angle = partSpringOffset.Magnitude
part.CFrame *= CFrame.fromAxisAngle(axis,angle) * CFrame.Angles(0, math.rad(180), 0) * gunCfOffsetVal.Value * gunShotVal.Value
end)
Y’ello, even though we PM’ed each other here’s the improved spring view model example I made in this other post with view model clamping and it fixes the weapon disappearing due to nan error. CC @omgcchheeessee. Make sure to not use a humanoid for animation as we discussed and use an animation controller.
The only CFrame part you should change is the original goal CFrame
part.CFrame = goalCFrame
Don’t touch the spring CFrame multiplication afterwards that is whats causing the CFrame to rotate.
local part = Instance.new("Part")
part.Size = Vector3.new(0.5,0.5,2)
part.Anchored = true
part.CanCollide = false
part.Parent = workspace
local cameraOffset = CFrame.new(0.8, -0.6, -2.2)
local camera = workspace.CurrentCamera
local RunService = game:GetService("RunService")
local Spring = require(script:WaitForChild("Spring"))
local ZEROVECTOR =Vector3.new()
local viewmodelSpring = Spring.new(ZEROVECTOR)
viewmodelSpring.Speed = 5
viewmodelSpring.Damper = 0.25 --1 is perfect dampening
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 --degrees
local previousGoalCFrame = CFrame.new()
RunService.RenderStepped:Connect(function(step)
local goalCFrame = camera.CFrame*cameraOffset
part.CFrame = goalCFrame
--Spring stuff
local differenceCF = previousGoalCFrame:ToObjectSpace(goalCFrame)
local axis, angle = differenceCF:ToAxisAngle()
local angularDisplacement = axis*angle
previousGoalCFrame = goalCFrame
local springForce = angularDisplacement*deltaSensitivity
viewmodelSpring:Impulse(springForce)
local partSpringOffset = viewmodelSpring.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 = viewmodelSpring.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
viewmodelSpring: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
part.CFrame *= CFrame.fromAxisAngle(axis,angle)
end
end)