Buggy View model script using :lerp()

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?

1 Like

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()

Lerp and Tween are not the same. Tween gets something from A to B. Start to Finish. When you Lerp something you are taking it from A to B in an interval of sorts. using :Lerp(Stuff,1) Will set it instantly to its destination while using :Lerp(Stuff,0) wont move it at all. Essentially by using lerp all you are doing is moving it partially to its destination and by running it in a loop you are moving it over and over. An example below:

I think the best solution would be to use an actual Animation for the arm swaying and find another method to attach the arms to the camera.

If I call :Lerp(Stuff,.5) It will go half way. I then call :Lerp(Stuff,.5) again and now its 3/4 of the way to its destination. Because it originally when half way, and then it went half of that which is 1/4. 1/2 + 1/4 = 3/4. The more you call this the closer and closer it will get to its destination.

So essentially I need to use tween service then. Sway animations I don’t see how that could be made dynamically when they are preset animations. I wouldn’t actually think it would be this hard to implement. I obviously switched from setprimarypart to welding to camera with motor6d and weld constraints.

Do you think I just need to make a part that is always at camera thats invisible and attach the rig to that part and move the rig around than the camera part. I would think that would make things complicated then it needs to just for sway and I would have similar issues. Tween service will have to be better and maybe I can get the sway to work properly. ObjectSpace didn’t seem to work well and ToOrientation also.

Thanks for replying this thing is getting old to me but I’m determined to get it working because it makes it look a little more polished. Btw the breathing is sine waves so the only animation is holding gun.