I am currently working on a FPS game and my goal is to make the fps arms move back when the gun is fired (essentialy I want recoil). I have tried using animations, which did not work, because it did not move the gun with the arms, and I can’t figure out how to do the springs-thing. The code is in ReplicatedStorage>Modules>Guns>Handler, near the bottom.
Should I be using the springs module to do this, or an animation? The only problem with the animation is that the gun does not move with the arms, so I think I should use springs, but I can’t quite wrap my head around how I am supposed to do this.
I get what you are saying, but am still confused on how to incorporate it. I know it must be in this function, but I’m not sure how exactly. Could you help me with this? Also this function is hooked up to a RenderStepped.
if self.viewmodel then
-- get velocity for walkCycle
local velocity = self.character.PrimaryPart:GetVelocityAtPosition(self.character.PrimaryPart.Position)
-- it'll be final for a reason. You'll see!
local finalOffset = self.viewmodel.offsets.idle.Value
-- Let's get some mouse movement!
local mouseDelta = game:GetService("UserInputService"):GetMouseDelta()
local shoveY = 0
if self.character:FindFirstChildOfClass("Humanoid").FloorMaterial == Enum.Material.Air then
shoveY = mouseDelta.y/150
end
self.springs.sway:shove(Vector3.new(math.clamp(mouseDelta.x,-MOUSE_DELTA_RANGE,MOUSE_DELTA_RANGE) / 200,shoveY)) --not sure if this needs deltaTime filtering
-- speed can be dependent on a value changed when you're running, or standing still, or aiming, etc.
-- this makes the bobble faster.
local speed = 3
-- modifier can be dependent on a value changed when you're aiming, or standing still, etc.
-- this makes the bobble do more. or something.
local modifier = 0.05
-- See? Bobbing! contruct a vector3 with getBobbing.
local movementSway = Vector3.new(getBobbing(10,speed,modifier),getBobbing(5,speed,modifier),getBobbing(5,speed,modifier))
-- if velocity is 0, then so will the walk cycle
self.springs.walkCycle:shove((movementSway / 25) * dt * 60 * velocity.Magnitude)
-- Sway! Yay!
local sway = self.springs.sway:update(dt)
local walkCycle = self.springs.walkCycle:update(dt)
--ToWorldSpace basically means rootpart.CFrame = camera CFrame but offset by xxx while taking rotation into account. I don't know. You'll see how it works soon enough.
self.viewmodel.handle.CFrame = self.camera.CFrame:ToWorldSpace(finalOffset)
self.viewmodel.rootPart.CFrame = self.viewmodel.rootPart.CFrame:ToWorldSpace(CFrame.new(walkCycle.x / 2,walkCycle.y / 2,0))
-- Rotate our rootpart based on sway
self.viewmodel.rootPart.CFrame = self.viewmodel.rootPart.CFrame * CFrame.Angles(0,-sway.x,sway.y)
self.viewmodel.rootPart.CFrame = self.viewmodel.rootPart.CFrame * CFrame.Angles(0,walkCycle.y,walkCycle.x)
end
end```
Actually your function is mean to be fired when you move your gun so when u move your gun it will sway so the script i gave you is actually mean to move right and left when u walk also the script seem to be complicated try making a gun with basic code here is a video: How to Make a Roblox FPS Framework (Part 2) - YouTube