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.
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.
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
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.
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.
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.
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.
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.