Hello developers!
I’m having problems whit two camera scripts, I don’t know about this type of math so I decided to create a post about this.
First camera script:
local char = script.Parent
local root = char:WaitForChild("HumanoidRootPart")
local hum = char:WaitForChild("Humanoid")
game:GetService("RunService").RenderStepped:Connect(function(DT)
local Tick = tick()
if (root.Velocity * Vector3.new(1,0,1)).Magnitude > 1 then
local velocity = (root.Velocity * Vector3.new(1,0,1)).Magnitude
local X = math.cos(Tick * (velocity / 2.8)) * 1.3
local Y = math.abs(math.sin(Tick * (velocity / 2.8)) * 1.2)
hum.CameraOffset = hum.CameraOffset:Lerp(Vector3.new(X, Y, 0), 0.25)
else
hum.CameraOffset *= 0.75
end
end)
The script works, the problem is the script makes something strange when the humanoid walkspeed tweens.
Video:
robloxapp-20230501-0954306.wmv (4,7 MB)
Second camera script:
local runService = game:GetService("RunService")
local plr = game.Players.LocalPlayer
local chr = plr.Character
local humrootpart = chr:WaitForChild("HumanoidRootPart")
local hum = chr:WaitForChild("Humanoid")
local cam = workspace.Camera
local tiltSpeedZ = -0.06
local bobbingSpeed = 0.1
local tilt = 0
local sinValue = 0
function lerp(a, b, t)
return a + (b - a) * t
end
function calculateSine(speed, intensity)
sinValue += speed
if sinValue > (math.pi * 1) then sinValue = 0 end
local sineY = intensity * math.sin(1 * sinValue)
local sineX = intensity * math.sin(sinValue)
local sineCFrame = CFrame.new(sineX, sineY, 0)
return sineCFrame
end
local previousSineX = 0
local previousSineY = 0
runService.RenderStepped:Connect(function(dt)
local movementVector = cam.CFrame:vectorToObjectSpace(humrootpart.Velocity / math.max(hum.WalkSpeed, 0.01))
local speedModifier = (hum.WalkSpeed / 30)
tilt = math.clamp(lerp(tilt, movementVector.Z * tiltSpeedZ, 0.1), -0.25, 0.1)
local sineCFrame = calculateSine(bobbingSpeed * speedModifier, movementVector.Z * speedModifier)
local lerpedSineX = lerp(previousSineX, sineCFrame.X, 0)
local lerpedSineY = lerp(previousSineY, sineCFrame.Y, 0)
cam.CFrame *= CFrame.Angles(-tilt, 0, 0) * CFrame.new(lerpedSineX, lerpedSineY, 0)
previousSineX = lerpedSineX
previousSineY = lerpedSineY
end)
This script I took it from the toolbox, I just changed some stuff to make that the camera tilts depending if the player is moving forwards or backwards.
This is what should happen:
robloxapp-20230501-1000231.wmv (3,0 MB)
But this is what happens:
robloxapp-20230501-0956277.wmv (4,1 MB)
If you know how to solve or you think you know how to solve this please let me know.
Edit : Forgot to say that this is my first post, I hope you understand it.