Let’s say that we already have the animation loaded.
You could use:
while true do
wait()
if not game.Players.LocalPlayer.Character:WaitForChild("Humanoid").MoveDirection == Vector3.new(0, 0, 0) then
if not animation.IsPlaying then
animation:Play()
end
else
animation:Stop
end
end
I assume you’re using a viewmodel with the gun and arms all attached together?
You could store a CFrameValue to use as an animation offset, and you can manipulate its value when the player begins moving. CFrame the viewmodel at its base position (in front of the camera or whatever) and then multiply it by the CFrameValue.
You can use the sin and cos functions to get X and Y points you can use as a direction vector, multiply them to change the amplitude or radius.
local a = 0
local speed = 5
local radius = 2
game:GetService("RunService").RenderStepped:Connect(function(dt)
a += dt*speed
local X, Y = math.cos(a)*radius, math.sin(a)*radius
--//Use X and Y as an offset
end)
So I have been tweaking with the code, and I can’t seem to NOT get the “Vector3 expected, got CFrame” error, or in between. Anything I’m doing wrong here?
local runservice = game:GetService("RunService")
local RepFirst = game.ReplicatedFirst
local Cam = workspace.CurrentCamera
local CamPos = Cam.CFrame
local Plr = game.Players.LocalPlayer
local Chr = Plr.CharacterAdded:Wait()
local Model = RepFirst.Luger
local Tool = script.Parent
local isRunning
Tool.Equipped:Connect(function()
Luger = Model:Clone()
Luger.Parent = workspace
local GunPos = Luger.CameraBone
local a = 0
local speed = 5
local radius = 2
running = game:GetService("RunService").RenderStepped:Connect(function(dt)
a += dt*speed
GunPos.CFrame = CFrame.new(Cam.CFrame)
local X, Y = math.cos(a)*radius, math.sin(a)*radius
GunPos.CFrame = CFrame.new(Cam.CFrame * CFrame.new(X,Y,0))
end)
end)
Tool.Unequipped:Connect(function()
Luger:Destroy()
running:Disconnect()
end)
It works, it just keeps moving in circles. How do I make it move in a motion?
Edit: How do I make it work when the player runs? And I need to lerp the gun to the Camera position as well. I’ve tried to figure that out but I haven’t gotten it to work. It just stays in the air. Heres my code:
local runservice = game:GetService("RunService")
local RepFirst = game.ReplicatedFirst
local Cam = workspace.CurrentCamera
local CamPos = Cam.CFrame
local Plr = game.Players.LocalPlayer
local Chr = Plr.CharacterAdded:Wait()
local Model = RepFirst.Luger
local Tool = script.Parent
local isRunning = false
Tool.Equipped:Connect(function()
Luger = Model:Clone()
Luger.Parent = workspace
local GunPos = Luger.CameraBone
local a = 0
local speed = 5
local radius = 2
while true do
wait()
if not game.Players.LocalPlayer.Character:WaitForChild("Humanoid").MoveDirection == Vector3.new(0, 0, 0) then
if not isRunning then
isRunning = true
running = game:GetService("RunService").RenderStepped:Connect(function(dt)
a += dt*speed
GunPos.CFrame = CFrame.new(Cam.CFrame.Position)
local X, Y = math.cos(a)*radius, math.sin(a)*radius
GunPos.CFrame = Cam.CFrame * CFrame.new(X,Y,0)
end)
end
else
isRunning = false
end
end
end)
Tool.Unequipped:Connect(function()
wait(1)
Luger:Destroy()
end)