Recoil Not Resetting Properly

Hello, I’m working on a VR game and have added a pistol and recoil to it. Currently the recoil works as follows: I apply an animation to the hand which pushes it back and angles it up slightly, I also change an integer “yOffset” which I use to apply a scaling rotation on the right hand. This all works fine and it scales properly with each shot however the yOffset isn’t being correctly set to my default yOffset after the player stops shooting for long enough. This ends up leading to the hands orientation being incorrect which feels wrong, and messes with accuracy.

My Code is as follows:

local function recoil()
	if twoHanding == true then
		local anim = rightHand.Parent.Glock.AnimationController.Animator.Shoot
		local Shoot = rightHand.Parent.Glock.AnimationController.Animator:LoadAnimation(anim)
		local hand = rightHand.Parent.AnimationController.Animator:LoadAnimation(rightHand.Parent.AnimationController.Animator.ShootPistolTwo)
		hand:Play()
		Shoot:Play()
		if currentShot - lastShot > maxWait then
			lastShot = currentShot
			while yOffset < 90 do
				print(yOffset.." | "..desiredOffset)
				yOffset += 2.5
				desiredOffset += 2.5
				task.wait(0.02)
			end	
		else
			while yOffset > desiredOffset do
				print(yOffset.." | "..desiredOffset)
				yOffset -= 2.5
				task.wait(0.01)
			end
		end		
	else
		local anim = rightHand.Parent.Glock.AnimationController.Animator.Shoot
		local Shoot = rightHand.Parent.Glock.AnimationController.Animator:LoadAnimation(anim)
		local hand = rightHand.Parent.AnimationController.Animator:LoadAnimation(rightHand.Parent.AnimationController.Animator.ShootPistol)
		hand:Play()
		Shoot:Play()
		if currentShot - lastShot > maxWait then
			lastShot = currentShot
			while yOffset < 90 do
				print(yOffset.." | "..desiredOffset)
				yOffset += 2.5
				desiredOffset += 2.5
				task.wait(0.02)
			end	
		else
			while yOffset > desiredOffset do
				print(yOffset.." | "..desiredOffset)
				yOffset -= 2.5
				task.wait(0.01)
			end
		end		
	end

end

local function shoot()
	if ammo >= 1 then
		ammo -= 1
		if twoHanding == true then
			local flashCoro = coroutine.create(Flash)
			coroutine.resume(flashCoro)
			local soundCoro = coroutine.create(sound)
			coroutine.resume(soundCoro)
			makeBullet()
			local offsetMath = math.clamp(desiredOffset - 2,25,maxOffset)
			desiredOffset = offsetMath
			local recoilCoro = coroutine.create(recoil)
			coroutine.resume(recoilCoro)
			currentShot = tick()
			if currentShot - lastShot > maxWait then
				lastShot = currentShot
				while yOffset < 90 do
					print(yOffset.." | "..desiredOffset)
					yOffset += 2.5
					desiredOffset += 2.5
					task.wait(0.02)
				end	
			end
			if ammo <= 0 then
				while yOffset < 90 do
					print(yOffset.." | "..desiredOffset)
					yOffset += 2.5
					desiredOffset += 2.5
					task.wait(0.02)
				end	
			end
		else
			local flashCoro = coroutine.create(Flash)
			coroutine.resume(flashCoro)
			local soundCoro = coroutine.create(sound)
			coroutine.resume(soundCoro)
			makeBullet()
			local offsetMath = math.clamp(desiredOffset - 5,25,maxOffset)
			desiredOffset = offsetMath
			local recoilCoro = coroutine.create(recoil)
			coroutine.resume(recoilCoro)
			currentShot = tick()
			if currentShot - lastShot > maxWait then
				lastShot = currentShot
				while yOffset < 90 do
					print(yOffset.." | "..desiredOffset)
					yOffset += 2.5
					desiredOffset += 2.5
					task.wait(0.02)
				end	
			end
			if ammo <= 0 then
				while yOffset < 90 do
					print(yOffset.." | "..desiredOffset)
					yOffset += 2.5
					desiredOffset += 2.5
					task.wait(0.02)
				end	
			end
		end
	end

end

Thanks in advance :slight_smile:

1 Like