Lock ViewModel arms at certain height

I’m trying to get a viewmodel system like criminality, It moves freely and stops going up at a certain point. However when I tried to do this (and I’ve been trying for like 2 hours), it never works as intended.

I’ve recently gotten pretty close actually, however another error emerges.

-- Pivot the arms to the camera, targetCFrame is the offset plus movement sway, swayCFrame is left and right sway
viewmodel:PivotTo(camera.CFrame * (targetCFrame * swayCFrame))
ArmHeight() -- calling my function to make the arms not go too high
function ArmHeight()
	local target = hrp.CFrame.Position.Y -- HumanoidRootPart point, 
	local actual = viewmodel.ArmPos.CFrame.Position.Y -- ViewModel highest arm point
	if tonumber(actual) > tonumber(target) then
		viewmodel:PivotTo(CFrame.new(viewmodel.PrimaryPart.CFrame.Position.X, hrp.CFrame.Position.Y + 1, viewmodel.PrimaryPart.CFrame.Position.Z))
	end
end

The problem is it’s locking up in a certain direction, I’m sure whatever I’m doing wrong is obvious but I’m just not seeing it, Video of what’s happening :

Video of what I want to happen/replicate :

1 Like

Btw, SetPrimaryPartCFrame does the exact same thing, I already tried swapping the PivotTo with that, same result.

1 Like

The second video isn’t loading for me, here’s it again

1 Like

You mean you want to limit the Up/Pitch rotation of the viewmodel?

I’m pretty sure that was what criminality did (I might be wrong)

1 Like

Well yeah, I’m trying to make it so the arms stop going up at a certain height, I posted about all my relevant script and the bug I’m getting

this should work

local MaxUpRotation = 20  -- change to your likings

local targetcframe = workspace.CurrentCamera.CFrame
local Pitch,_,_ = targetcframe:ToOrientation()

Pitch = math.min(Pitch,math.rad(MaxUpRotation))

viewmodel.CFrame = torso.CFrame * CFrame.Angles(Pitch,0,0)

This doesn’t work, and even after I fixed it it’s just not what I’m looking for.

You see, what I’m trying to replicate is a regular viewmodel, except it stops going up with my camera past a point.

If I look straight, my arms are pointed straight. if I look down, they point down, but if I look up, I want them still pointed straight. That’s exactly how criminality has it, the running animation is the same looking straight as looking down, but if you look up then your arms stop going up.

I should probably elaborate, it doesn’t work because viewmodel is a model, and as such does not have a CFrame. if I do the primary part CFrame, the arms along with it just fly off into the distance, how I fixed it is using the same :PivotTo

This just makes the arms a certain height (which technically is what I want, it’s just not moving around at all.). Aswell as the sway being buggy, but I’m sure that’s fixable.

This is how I had to modify the script to get it working.

function ArmHeight()
	local MaxUpRotation = 5  -- change to your likings (did nothing, 5 and 20 still the same)

	local targetcframe = workspace.CurrentCamera.CFrame
	local Pitch,_,_ = targetcframe:ToOrientation()

	Pitch = math.min(Pitch,math.rad(MaxUpRotation))
	
	local newpos = CFrame.new()
	newpos = newpos * hrp.CFrame * CFrame.Angles(Pitch,0,0)

	viewmodel:PivotTo(newpos)
end

I’m going to assume that my “fix” to the script just broke it. I’ll also provide a video of what happens if I use your script except I change the primarypart CFrame.


Like I said, they just fly into oblivion.

Okay correction, the viewmodel primary part is staying still, the arms are just flying off. Let me see if I can find out why

Well I fixed it and it does the exact same thing, the welds were just weird.

function ArmHeight()
	local MaxUpRotation = 5  -- change to your likings (did nothing, 5 and 20 still the same)

	local targetcframe = workspace.CurrentCamera.CFrame
	local Pitch,_,_ = targetcframe:ToOrientation()

	Pitch = math.min(Pitch,math.rad(MaxUpRotation))
	
	local newpos = CFrame.new()
	newpos = newpos * hrp.CFrame * CFrame.Angles(Pitch,0,0)

	viewmodel:PivotTo(newpos)
end

In here, the hrp which was previously called torso (in the original script that i sent) MUST be the player character’s torso, and not the viewmodel’s

Yes, hrp is the players humanoidrootpart, which all players have regardless of r15 or r6,
same exact problem as before.

function ArmHeight()

local viewmodeloffset = Vector3.new(0,0,0) -- Change to whatever you want
	local MaxUpRotation = 5  -- change to your likings (did nothing, 5 and 20 still the same)

	local targetcframe = workspace.CurrentCamera.CFrame
	local Pitch,_,_ = targetcframe:ToOrientation()

	Pitch = math.min(Pitch,math.rad(MaxUpRotation))
	
	local newpos = CFrame.new((hrp.CFrame * CFrame.Angles(Pitch,0,0))*CFrame.new(viewmodeloffset))

	viewmodel:PivotTo(newpos)
end

here, try this

Thank you, this is so close to the exact output I wanted, however the sway is now choppy, I again had to change your code a little bit, when you add CFrames it’s much easier to lerp from my experience, here’s the new code

function ArmHeight()
local viewmodeloffset = Vector3.new(0,0,0) -- Change to whatever you want
	local MaxUpRotation = 5  -- change to your likings (did nothing, 5 and 20 still the same)

	local targetcframe = workspace.CurrentCamera.CFrame
	local Pitch,_,_ = targetcframe:ToOrientation()

	Pitch = math.min(Pitch,math.rad(MaxUpRotation))
	
	local newpos = CFrame.new()
	newpos = newpos:Lerp(hrp.CFrame * CFrame.Angles(Pitch,0,0)*CFrame.new(viewmodeloffset), 1)

	viewmodel:PivotTo(newpos)
end

Video representation of the problem :

Thank you again, this is so close and if you don’t want to help me further I completely understand

That’s because the lerping speed of 1 is WAYYY too much

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.