im making a game for my friend but i need help making the camera sway while in idle
could you go into more detail on what you want the camera to actually do? “sway” is kinda confusing
Like you know when your in idle the camera starts moving like slowly
?
Like in random first person games
Do you have a video example of this? Random first person games
isn’t very specific.
I want camera movement like this game i guess
You can try using this code.
local RunService = game:GetService("RunService")
local camera = workspace.CurrentCamera
local speed = 3
local intensity = 0.5
local focusIntensity = 1.1
local smoothness = 0.2
RunService.RenderStepped:Connect(function()
local t = tick()
local x = math.cos(t * speed) * intensity
local y = math.sin(t * speed) * intensity
local cf = CFrame.new(Vector3.new(x, y, 0), Vector3.new(x*focusIntensity, y*focusIntensity, -5)) + camera.CFrame.Position
camera.CFrame = camera.CFrame:Lerp(cf * camera.CFrame.Rotation, smoothness)
end)
Video:
I think i need to change the settings
this should work better
local RunService = game:GetService("RunService")
local camera = workspace.CurrentCamera
local speed = 3
local intensity = 0.5
local smoothness = 0.2
RunService.RenderStepped:Connect(function()
local t = tick()
local x = math.cos(t * speed) * intensity
local y = math.sin(t * speed) * intensity
local cf = CFrame.new(Vector3.new(x, y, 0), Vector3.new(x*0.95, y*0.95, -10)) + camera.CFrame.Position
camera.CFrame = camera.CFrame:Lerp(cf * camera.CFrame.Rotation, smoothness)
end)
That worked good! now I just need to fix the walk movement mixed with the idle movement
What did you mean by that? Is it related to the script I sent you?
They only want this effect when the player’s not moving as mentioned in the original text
I think they have enough to go from here, however. You give a man a fish, you feed them for 1 day. You teach a man to fish, you feed them a lifetime. In this case, you give them code and it only works for them in this case. Teach them what it does, and they’ll be able to do it on their own from then on.
I didn’t have a lot of time to explain everything.
So I will explain it now.
First, I will explain what I was using to achieve this effect.
Now, how do we start? I will start by defining variables. We will need RunService.RenderStepped for lerps because they should run in a loop. And as you may know, the RenderStepped function fires every frame. We will also need to define the camera to display our sway.
local RunService = game:GetService("RunService")
local camera = workspace.CurrentCamera
Let’s define the configuration variables now.
local speed = 3
local intensity = 0.5
local smoothness = 0.2 -- lerp's second argument, should be a number from 0 to 1. if it's closer to 0 then the lerp will be smoother
This will create a new RenderStepped loop.
RunService.RenderStepped:Connect(function()
end)
Now the most confusing part is about CFrames and trigonometric functions. Sorry, but I won’t explain how they work.
local t = tick() -- we can define tick by calling it as a function
local x = math.cos(t * speed) * intensity
local y = math.sin(t * speed) * intensity
You can set CFrame’s focus on another Vector3 by using CFrame.lookAt
or CFrame.new
. The first argument is the start position and the second argument is the focus position.
local start = Vector3.new(x, y, 0)
local focus = Vector3.new(x*0.95, y*0.95, -10) -- the third variable will position the focus vector behind the start vector. We multiply x and y by 0.95 to make x and y a little bit smaller.
-- now just make a new cframe. and then just add the old position of the camera so it stays where it was
CFrame.new(start, focus) -- like this
--or just shorten everything like I did
local cf = CFrame.new(Vector3.new(x, y, 0), Vector3.new(x*0.95, y*0.95, -10)) + camera.CFrame.Position
Then just use the lerp function. And add the camera’s old rotation so the player can freely rotate the camera.
camera.CFrame = camera.CFrame:Lerp(cf * camera.CFrame.Rotation, smoothness)
To make so it only plays on idle use:
It would look like this
-- let's define our character and if it's not loaded we wait so it loads
local Players = game:GetService("Players")
local character = Players.LocalPlayer.Character or Players.LocalPlayer.CharacterAdded:Wait()
-- now we will need to define the Humanoid
local humanoid = character:WaitForChild("Humanoid")
-- then check if the player is moving to set x and y to 0
if humanoid.MoveDirection.Magnitude > 0 then
x = 0
y = 0
end
The final code
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local camera = workspace.CurrentCamera
local speed = 3
local intensity = 0.5
local smoothness = 0.2
RunService.RenderStepped:Connect(function(deltaTime)
local character = Players.LocalPlayer.Character or Players.LocalPlayer.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local t = tick()
local x = math.cos(t * speed) * intensity
local y = math.sin(t * speed) * intensity
local z = -10
if humanoid.MoveDirection.Magnitude > 0 then
x = 0
y = 0
z = 0
end
local cf = CFrame.new(Vector3.new(x, y, 0), Vector3.new(x*0.95, y*0.95, z)) + camera.CFrame.Position
camera.CFrame = camera.CFrame:Lerp(cf * camera.CFrame.Rotation, smoothness)
end)
The answers my problem, Thank you so much
How would you make it smoothly start and stop swaying?
I Think that you can locally slowly tween the values to 0 each time the Magnitude of the Humanoid MoveDirection is changed from 0-1
Quick question, would you need to use a local script or script and where would this script be placed? Sorry if this question has already been answered else where, I’m still fairly new to dev forum.
That would be in a local script in StarterCharacterScripts