Buggy View model script using :lerp()

I am trying to get my view model to have smooth sway from moving camera any direction instead of depth so front and back.

The script dose not look smooth and kind of glitched when moving.

when i put the last value to 1 it makes the model still but i want to have sway is there an alternate method for this sway with the camera or is there a fix for this that i dont know of.
i am also trying to not expose my hole script to the world so i slimmed it down.

This is under RenderStepped atm.

Setprimarypartcframe is not the best solution but its good for me for now until i figure a way to do all of this in a different way.

ViewmodelArms:SetPrimaryPartCFrame(ViewmodelArms:GetPrimaryPartCFrame():lerp(Lcamera.CFrame * CFrame.new(x,y,z)* CFrame.fromEulerAnglesXYZ(xr,yr,zr)),0))

ive tried separating getprimarypartcframe and putting it into heartbeat but it dose not seem to solve it. i looked all over the forums and google and got no solution.

1 Like

The game seems to be set to private.

I fixed the place not opening for others just note at some time when this gets resolved it will be set private later on.

What exactly do you mean by view model?

if you are looking for smooth gun swaying this might be a solution:

Also as i a Side note i would recommend using render stepped/BindToRenderStepped for most things camera related, not heartbeat…

2 Likes

I must of miss said I am using Renderstepped
I am using sine logic in heartbeat though.

I’ll have to rework what I wrote. The script I did example of is a quarter of what the size is.

I’ll have to reply later to see if it works.

Edit: Viewmodel I was meaning was Rig model it’s just that some people called it viewmodel and I liked it so ¯_(ツ)_/¯

Your issue is that you are using lerp() to place your model at the camera position. Because lerp() isn’t an instant snap, your model lags behind when the camera moves. To fix your unwanted behavior, stop using lerp() and just snap your viewmodel’s position to the camera’s position.

model:SetPrimaryPartCFrame(workspace.CurrentCamera.CFrame)

Why would lerp be causing that then isn’t it supposed to smoothly transition like tween service.

The reason I was using lerp was to make it sway like the gun was trying to catch up but it’s resulting into jiddered animation.

So doing the method @Jaycbee05 has said would be combined with this wouldn’t it?

Thanks for that but, don’t I need to getprimarypart
To get the position of the primary part then add it with cframe.new and cframe.angles?

i couldnt get what they said to work but it did fix the thing of using Primarypartcframe

but the script when i edit it dosent seem to work im unsure what they mean by camera delta but this is what i got so far

Lcamera is the camera position hinting L for LocalCamera and the ViewmodelArms is the rig. for swayoffsetting i tryed to manually specify the location the the primary part incase it wasnt reading it but i get a error saying

Cframe is not a Valid member of part…

local mult = 1

function renderloop() --bind this camera render loop

local rotation = Lcamera.CFrame:toObjectSpace(Lcamera.CFrame) --get cframe delta.

local xs,ys,zs = rotation:ToOrientation() --I’m sure there are better ways to get rotation but this will work for now.

local swayOffseting = Arms.PrimaryPart.Cframe

local swayOffset = swayOffseting:Lerp(CFrame.Angles(math.sin(xs)*mult,math.sin(ys)*mult,0), 0.1) --calculate the sway using SIN

ViewmodelArms.CFrame = ViewmodelArms.CFrame * swayOffset --apply the sway

Lcamera = Lcamera.CFrame --update the last cframe

end

renderloop()

You are using Cframe instead of CFrame

local swayOffseting = 
 Arms.PrimaryPart.CFrame

Also it would help if you formatted your
code ( understandable if you can’t)

It’s kind of adapting to be better as I go on. As you saw I was trying to do work around a to get it working

1 Like

Also if you are wondering in this case “CFrame delta” is CFrame:ToObjectSpace, were the CFrame is transformed from Object to World space. Equivalent to [CFrame:inverse() * cf] ( straight from the wiki), then by inserting the last camera cframe (parameter) they are getting the

Delta

im unsure on how to fix this but i get a error swayOffset is a nil value and on that post it dose not tell what it has because it cant be call its self it would come nil how would i solve this?

local mult = 1
function renderloop() --bind this camera render loop

local rotation = ViewmodelArms.PrimaryPart.CFrame:toObjectSpace(Lcamera.CFrame) --get cframe delta.

local x,y,z = rotation:ToOrientation() --I’m sure there are better ways to get rotation but this will work for now.

swayOffset = swayOffset:Lerp(CFrame.Angles(math.sin(x)*mult,math.sin(y)*mult,0), 0.1) --calculate the sway using SIN

ViewmodelArms.PrimaryPart.CFrame = ViewmodelArms.PrimaryPart.CFrame * swayOffset --apply the sway

Lcamera.CFrame = workspace.CurrentCamera.CFrame --update the last cframe

end
renderloop()

Where exactly is it saying SawOffset is a nil value(line, piece of code)?

This is the line sorry to bother a lot but I’ve been at doing the rig and model for a month with not much progress.

The rig literally spazzing.
I’m guessing I have to define swayOffset to something but I’m unsure what to set to I tried doing empty cframe and number 0 and also tried setting it to currentcamera.

swayOffset = swayOffset:Lerp(CFrame.Angles(math.sin(x)*mult,math.sin(y)*mult,0), 0.1) --calculate the sway using SIN

For some reason it comes with the same message maybe it dose not work that way anymore?
i notice that when i make
local swayOffset = CFrame.new()

it starts working but it dose not attach to camera now wich i think is odd.

Have you tried something like this yet?

local  swayOffset = CFrame.Angles(math.sin(x)*mult,math.sin(y)*mult,0)
swayOffset = swayOffset:Lerp(CFrame.Angles(math.sin(x)*mult,math.sin(y)*mult,0), 0.1)
gun.CFrame = gun.CFrame * swayOffset

or

local  swayOffset = 0
swayOffset = swayOffset:Lerp(CFrame.Angles(math.sin(x)*mult,math.sin(y)*mult,0), 0.1)
gun.CFrame = gun.CFrame * swayOffset

the one with the 0 breaks the command and the top one does same thing as me putting cframe.new in it.

up date on what i have so far

local mult = 1
function renderloop() --bind this camera render loop

local rotation = ViewmodelArms.PrimaryPart.CFrame:toObjectSpace(Lcamera.CFrame) --get cframe delta.

local x,y,z = rotation:ToOrientation() --I’m sure there are better ways to get rotation but this will work for now.
local swayOffset = CFrame.new()

swayOffset = swayOffset:Lerp(CFrame.Angles(math.sin(x)*mult,math.sin(y)*mult,0), 0.1) --calculate the sway using SIN

ViewmodelArms.PrimaryPart.CFrame = ViewmodelArms.PrimaryPart.CFrame * swayOffset --apply the sway

Lcamera.CFrame = workspace.CurrentCamera.CFrame --update the last cframe

end
renderloop()

btw i updated the game if you need to see what happening

Alright, So for now i am going to leave you with a topic that is very similar(its basically the same method) to what we are doing here(this should help fix the problem): https://devforum.roblox.com/t/gun-sway-how-to/23726/2 and this:
Gun Sway Issue, Jitter/Lag/Stutter

This might also be useful to look into:

as a recommendation for now i would look for some more methods and solutions on the devfourm(or anywhere) and research a little on some cframing and tweening, if you have any questions i would be glad to try and answer, when i can.
i’ll also do some of my own researching and testing at some point(if you or someone else hasn’t already solved this issue), and ill let you know what i find or recommend…

Have a great Day or Night

i have been referencing the first person element community tutorial but broke most of time and it didnt have the method of animating i wanted. i will just have to research even more or figure out a method. so i made it more easy for me to understand.

I have been researching about a week and a half and have not been able to fix it but lerp the item to the location of the camera wich is very jittery/laggy. you can visit the place to see how it looks.

function renderloop()

lastCameraCF = Lcamera.CFrame

local rotation = workspace.CurrentCamera.CFrame:toObjectSpace(lastCameraCF) --get cframe delta.
	local x,y,z = rotation:ToOrientation() --I'm sure there are better ways to get rotation but this will work for now.
		local Location = ViewmodelArms.PrimaryPart.CFrame
		--	local swayOffset = ViewmodelArms.PrimaryPart.CFrame		
		--	swayOffset = CFrame.Angles(math.sin(x)*mult,math.sin(y)*mult,0) --calculate the sway using SIN
			
		--	ViewmodelArms.PrimaryPart.CFrame = lastCameraCF * swayOffset
			
		
				ViewmodelArms.PrimaryPart.CFrame = lastCameraCF:Lerp(ViewmodelArms.PrimaryPart.CFrame * CFrame.new(OffsetAnimationSines)* CFrame.Angles(animationsines),mult/1.5) 
lastCameraCF = Lcamera.CFrame --update the last cframe

end

renderloop()