Gun swaying pushes character around

I have been trying to implement gun swaying, and I found a script that seems to do exactly what I want. I slightly modified it for it to work, and it appears to somewhat work correctly but it pushes the character around.
https://gyazo.com/e303cdc3a462b4c17bb41abfc4a03f0c

Here is the code:

local mult = 6
local lastCameraCF = Camera.CFrame
local swayOffset = CFrame.new()
function renderloop() --bind this camera render loop
	local rotation = Camera.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.
	swayOffset = swayOffset:Lerp(CFrame.Angles(math.sin(x)*mult,math.sin(y)*mult,0), 0.1) --calculate the sway using SIN
	Tool.Handle.CFrame = Tool.Handle.CFrame * swayOffset --apply the sway
	lastCameraCF = Camera.CFrame --update the last cframe
end

The function renderloop runs every frame. (RenderStepped)
How can I stop the character from being pushed around by the swaying of the gun?

How many Parts in the gun, and are the Parts set to massless?
If you have a lot of Parts or they have high Density the physics involved with moving that mass around may affect the Character.

I would suggest turning off CanCollide for all of the gun’s parts

2 Likes

CanCollide was set to false in the gif.

Only one part, and it is massless in the gif.

Hello,
I believe CanCollide is set to true on the laser gun. Turn that off (if there’s more parts with CanCollide on, turn that off too) and parent your gun to the CurrentCamera.

Wait. This is using an actual tool instead of a viewmodel? I don’t think you should be doing that. Use a viewmodel instead, as it’s way easier to configure and add items to that only the localplayer can see. The reason you’re being pushed around is most likely due to the weld from the tool moving your character.

1 Like

A viewmodel? In my years of programming, never heard of that yet. That’s very odd! I’ll look into it. If it is only a thing that the local player can see, how will other players see what they are holding?

It depends on how you pull it off. But yes, it is usually client sided. Unlike your script, viewmodels do not rely on a tool, but relies on your camera’s position and how you do your animations (meaning you can still have others see your tool while also hiding it on the client’s).

Ah, so in order for the character to appear as if they are holding the gun, I must use animations? Not too surprised. How would I attach the gun to the player? I assume using a weld would just result in this issue.

Also, as I am not an animator, do you know if there is anywhere I can get some free gun animations for Roblox? Tried searching around, couldn’t find any.

No, you can’t get any free gun animations nor do you have to use a weld. Like I said, viewmodels rely on the camera position since you’re parenting it to the CurrentCamera and setting the primary part of it’s position. You would have to use CFrame.new() to “attach” it to the player. Remember that viewmodels are only models that act like your gun, except the effects they do are only for your client.

1 Like

Alright, my apologies if I am seeming a bit “noobish”! This is one of my first attempts at scripting guns, I promise I am definitely not new to scripting on Roblox, haha. Not sure how I’ll handle animations so it appears the character is holding the gun, but I’ll try to see if I can find a way.

2 Likes

You should definitely start using Viewmodels. It doesn’t give you any problems like this unless you put a Humanoid in it.