Help with FPS viewmodel bobbing

Sorry for bumping this topic, but the 1 - (TweenService:GetValue) part just brings up an error “attempt to perform arithmetic (sub) on nil” why is get value nil?

You forgot to initialize startTime at line 3, I think

any idea how I would make this faster? Not higher or farther horizontally, just faster.

Make t increase faster, since it can only move at real time speed right now because it’s just t = tick(), you would have to use DeltaTime instead, do t += deltaTime * speed instead of t = tick()

2 Likes

Sorry for reviving this however I noticed that when you use alpha in this its always 1, meaning it will not tween the gun outwards however it will still quickly snap to the full walking movement. Any idea on how you could fix this?

How is it always one?

math.min(value, 1)

returns value or 1 if value is greater than 1, and in the case of my code, this value is ( tick() - startTime) / TweenTime which is the time elapsed since the start of the animation divided by the time required to tween, which essentially gives a value going up from 0 to 1 within TweenTime seconds (0.3 in our code).

The alpha value is just the same as the 0…1 value except it uses TweenService to make it look smoother, since the 0…1 value is linear.

https://gyazo.com/124248a8c9ed5d37197ffa4d838a56b6

In the video after using the same code your using but changing it to return the cframe so i can apply it to my ViewModel, It snaps when the player starts moving rather than tween between it.

Could you show me the full code? and could you try increasing TweenTime?

This is the Code I’m using where I return a CF, BobTime = 0.3 and it works fine for tweening back in.

local function BobCF()
	local cf = CFrame.new()

	if Humanoid.MoveDirection.Magnitude > 0 then
		t = tick()
		StartTime = 0
		
		local x = math.cos(t * 5) * 0.05
		local y = math.abs(math.sin(t * 5)) * 0.05
		
		local alpha = (TweenService:GetValue(math.min((tick() - StartTime) / BobTime, 1), Enum.EasingStyle.Cubic, Enum.EasingDirection.Out))
		cf = CFrame.new(x * alpha , y * alpha , 0)
	else
		if StartTime < 1 then StartTime = tick() end
		
		-- do math
		local x = math.cos(t * 5) * 0.05
		local y = math.abs(math.sin(t * 5)) * 0.05

		local alpha = 1- (TweenService:GetValue(math.min((tick() - StartTime) / BobTime, 1), Enum.EasingStyle.Cubic, Enum.EasingDirection.Out))
		cf = CFrame.new(x * alpha, y * alpha, 0)
	end
	
	return cf
end

You removed if lastState == 0 then lastState = 1 startTime = tick() end, It wasn’t there for no reason lol.

	if Humanoid.MoveDirection.Magnitude > 0 then
		t = tick()
		StartTime = 0

This code sets the StartTime to 0 every render step which just resets the tween every step, it should only be set to tick() (not 0) once the player goes from walking to running, which is why I used lastState.

Try this instead:

local lastState = 0
local function BobCF()
	local cf = CFrame.new()

	if Humanoid.MoveDirection.Magnitude > 0 then
		t = tick()
		if lastState == 0 then lastState = 1 startTime = t end
		
		local x = math.cos(t * 5) * 0.05
		local y = math.abs(math.sin(t * 5)) * 0.05
		
		local alpha = (TweenService:GetValue(math.min((t - StartTime) / BobTime, 1), Enum.EasingStyle.Cubic, Enum.EasingDirection.Out))
		cf = CFrame.new(x * alpha , y * alpha , 0)
	else
		if lastState == 1 then lastState = 0 startTime = tick() end
		
		-- do math
		local x = math.cos(t * 5) * 0.05
		local y = math.abs(math.sin(t * 5)) * 0.05

		local alpha = 1- (TweenService:GetValue(math.min((tick() - StartTime) / BobTime, 1), Enum.EasingStyle.Cubic, Enum.EasingDirection.Out))
		cf = CFrame.new(x * alpha, y * alpha, 0)
	end
	
	return cf
end

Thank you! Don’t know how I didn’t see that.

1 Like