Weird CFrame lerp glitch

Before you tell me this is similar to another issue someone had, I cannot do that, so please do not refer me to it. I’ve recently (almost) successfully made the weapons in my game have sway, so it looks natural. For some reason, when It get’s past a certain point, It starts glitching and bouncing. Here’s my code:

		local Position = Vector3.new(script.X.Value + CameraOffset.X + WeaponOffset.X,script.Y.Value + CameraOffset.Y + WeaponOffset.Y,script.Z.Value + CameraOffset.Z + WeaponOffset.Z)
		
		local Rx,Ry,Rz = (Camera.CFrame * CFrame.new(Position)):ToOrientation()
		
		WeaponModel:SetPrimaryPartCFrame(CFrame.new((Camera.CFrame * CFrame.new(Position)).Position))
		
		if Px and Py and Pz then
			WeaponModel:SetPrimaryPartCFrame(WeaponModel.PrimaryPart.CFrame:Lerp((CFrame.fromOrientation(Px,Py,Pz) + WeaponModel.PrimaryPart.CFrame.Position) * CFrame.fromOrientation(Rx,Ry,Rz),.5))
		end
		
		Px,Py,Pz = WeaponModel.PrimaryPart.CFrame:ToOrientation()

And here’s what happens:

Does anyone know what the hell is going on here?

I’m thinking it might be a problem with it the values going negative? I’m really not sure.

FYI: nothing else is interfering with it

I do know SetPrimaryPartCFrame is deprecated, but this sounds like it would be an issue with TweenService, but since you aren’t using that, I have no clue.

I had no Idea that was deprecated, but It’s very odd because THIS works:

local Position = Vector3.new(script.X.Value + CameraOffset.X + WeaponOffset.X,script.Y.Value + CameraOffset.Y + WeaponOffset.Y,script.Z.Value + CameraOffset.Z + WeaponOffset.Z)

WeaponModel:SetPrimaryPartCFrame(WeaponModel.PrimaryPart.CFrame:Lerp(Camera.CFrame * CFrame.new(Position),.5))

Which is almost the same thing, except it lerps position too.

Also, I did switch to PivotTo() Instead of set primary part cframe, and still got the same bug.

Try changing the lerp amount to 1 and see what happens.


I don’t even know how to explain what’s going on here

In that case, try converting some of your CFrame calculations into object space as that usually solves the issue.

Weird. I removed the lerping part and just did this:

		local Position = Vector3.new(script.X.Value + CameraOffset.X + WeaponOffset.X,script.Y.Value + CameraOffset.Y + WeaponOffset.Y,script.Z.Value + CameraOffset.Z + WeaponOffset.Z)
		
		local Rx,Ry,Rz = (Camera.CFrame * CFrame.new(Position)):ToOrientation()
		
		WeaponModel:PivotTo(CFrame.new((Camera.CFrame * CFrame.new(Position)).Position))
		
		if Px and Py and Pz then
			
			WeaponModel:PivotTo((CFrame.fromOrientation(Px,Py,Pz) + WeaponModel.PrimaryPart.CFrame.Position) * CFrame.fromOrientation(Rx,Ry,Rz))
			
			--WeaponModel:PivotTo(WeaponModel.PrimaryPart.CFrame:Lerp((CFrame.fromOrientation(Px,Py,Pz) + WeaponModel.PrimaryPart.CFrame.Position) * CFrame.fromOrientation(Rx,Ry,Rz),.5))
		end
		
		Px,Py,Pz = WeaponModel.PrimaryPart.CFrame:ToOrientation()

And it still did that BS glitch

Ever fix? I’m running into the same thing

OK I found a fix (at least in my case)…
Lerp() can end up returning NaN (not a number) for certain positions. I’m not sure why. Do a NaN check on your coordinates/rotation before you set the CFrame. You can check if any of the values are NaN just by comparing the lerped cframe to itself. NaN won’t ever equal itself

local desiredCFrame = CFrame.new():Lerp(Vector3, Vector3, alpha)
if desiredCFrame == desiredCFrame then -- Anything containing NaN won't be valid
      --Pivot your object
end

I wish I understood the math on this better

I wonder if OP solved their problem, because they didn’t use Lerp in the above post and it still had the issue.

I’m absolutely clueless.

  1. Check for errors or warnings in the output console. Your code may be producing runtime errors that are causing unexpected behavior. Make sure to check for errors or warnings in the output console and address them appropriately.
  2. Verify that your implementation of weapon sway is correct. Ensure that the sway values and offsets are applied correctly and consistently across the different axes (X, Y, Z) of the weapon model.
  3. Check if there are any conflicts with other scripts or objects in your game. It is possible that other scripts or objects are interfering with your weapon sway implementation and causing glitching and bouncing behavior. Try disabling other scripts or objects and see if the issue persists.
  4. Experiment with different interpolation methods. The code you provided uses a linear interpolation method (Lerp) to blend between the previous and current weapon orientations. You may want to try experimenting with different interpolation methods (such as Slerp or Cubic) to see if they produce smoother results.
  5. Try adjusting the parameters of your current implementation. You may want to try adjusting the speed of the interpolation (by modifying the second argument of the Lerp function), the range of sway values, or other parameters to see if they affect the glitching and bouncing behavior.

So sorry for not posting my solution! I ended up fixing it with this script:

		local Offset = CFrame.new(script.X.Value + CameraOffset.X + WeaponOffset.X,script.Y.Value + CameraOffset.Y + WeaponOffset.Y,script.Z.Value + CameraOffset.Z + WeaponOffset.Z)

		local FinalPosition = CFrame.new((Camera.CFrame * Offset).Position)

		local FinalRotation = WeaponModel.PrimaryPart.CFrame.Rotation:Lerp((Camera.CFrame * Offset).Rotation,.5)

		WeaponModel:PivotTo(FinalPosition * FinalRotation)

Note that the offset values can be changed and can be whatever you want. In my case it’s just an offset relative to my camera.

Also just a random comment this solution looks like it was generated with chatgpt

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