What do I want to achieve?
I want to make a script where when the player presses “D” to walk side ways, the PLAYER’S camera tilts on the “z” axis a little
Whats the issue?
Im not the best in scripting and this is my first time messing around with camera CFrame, yet this script does not work the way i want it to, it continuously tilts the camera (only on the “x” and “y” axis though, the “z” axis doesn’t work, please help with this) so the camera always ends up looking up at the sky
local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer
local Camera = workspace.CurrentCamera
local UserInputService = game:GetService("UserInputService")
function onInputChanged(inputObject, gameProcessedEvent)
if inputObject.KeyCode == Enum.KeyCode.D then
if inputObject.UserInputState == Enum.UserInputState.Begin then
while UserInputService:IsKeyDown(Enum.KeyCode.D) do
wait()
Camera.CFrame = Camera.CFrame * CFrame.Angles(0,0,math.rad(25))
end
end
end
end
UserInputService.InputBegan:Connect(onInputChanged)
What have I tried so far?
I have looked on a bunch of other dev forum topics, no one seems to say and/or know how to fix it so the camera can tilt on the “z” axis, I have tried to figure out CFrame:ToOrientation, wich, nothing I did worked, and more stuff that I cant remember.
Please help, I mostly just want to fix the tilting, because it’s wrong, the tilting on the “z” axis not working and I want to make the tilting smooth.
This rotates the camera on the world’s Z axis, not it’s own local Z axis. So if you’re looking in the direction of the world’s X axis, that would cause the camera to pitch instead of roll.
this works, just not the right way, it was probably my wording, but i dont want the camera to rotate around the player, i want the camera to tilt, Example, if I were to look at something with my head tilted sideways irl.
this is what i got from the script above btw (not what i want) and (im holding D btw in the video)
Here’s what I created in like 30 mins or so.
LocalScript:
wait(1)
local cam = workspace.CurrentCamera
local uis = game:GetService("UserInputService")
local rs = game:GetService("RunService")
local player = game:GetService("Players").LocalPlayer
local char
local ad = {0,0}
uis.InputBegan:Connect(function(inp, gpe)
if gpe then return end
if inp.KeyCode == Enum.KeyCode.D then
ad[1] = 1
elseif inp.KeyCode == Enum.KeyCode.A then
ad[2] = 1
end
end)
uis.InputEnded:Connect(function(inp)
if inp.KeyCode == Enum.KeyCode.D then
ad[1] = 0
elseif inp.KeyCode == Enum.KeyCode.A then
ad[2] = 0
end
end)
if player.Character then
char = player.Character
end
player.CharacterAdded:Connect(function(c)
char = c
end)
cam.CameraType = Enum.CameraType.Scriptable
rs.RenderStepped:Connect(function(dt)
cam.CFrame = char.Head.CFrame * CFrame.Angles(0, 0, (ad[2]-ad[1])*math.pi*0.1)
end)
That is the same thing as CFrame * CFrame, so Camera.CFrame *= CFrame.Angles(0,0,math.rad(25)) and Camera.CFrame *= Camera.CFrame:ToWorldSpace(CFrame.Angles(0, 0, rad(25))) are equivalent.
Smoothly tweening between 2 or more goal cframes that can change at random is something I’ve been looking into trying to do consistently timing-wise across various applications.
The basic idea is to use TweenService to tween it smoothly to the goal cframe. The goal cframe in the code I posted is set instantly. The issue I have is calculating the time it should take to get there. The time it takes to get there is based on where it currently is.
My problem aside, if you want something simple, then just use 0.5 seconds and it will look smooth.
:
Changing this line therefore creating a “modified” camera script would mean you can’t really use TweenService anymore. However you could move your character around while tilting the CFrame.
I believe this line is being ran in a RenderStepped variation.
Also if you don’t already know how to get the PlayerModule and CameraModule stuff, you simply press play in Studio and then look inside StarterPlayer.StarterPlayerScripts and Roblox’s scripts should be automatically inserted if it wasn’t there before.
I’m gonna be honest, your script is alien language to me because i am a very basic scripter, but it works, would there be a way to make the camera tilt a bit faster?
or whatever value you want. It’s the time in seconds to get to the goal cframe.
So
ad[3]
is the 3rd value in the array ad. And
ad[4]
is the 4th value.
The code treats the first two values as left/right tilt values to determine the goal cframe, which is just a modified cframe from whatever Roblox’s script is giving us.
The 3rd value is the time to get to the goal cframe.
Finally the 4th value is the current position of the cframe. So if ad[4] = ad[3] then you are at goal cframe, but if ad[4] = 0 then you are at non-tilting cframe.
ad[3] and ad[4] are both numbers in seconds.
ad[1] and ad[2] are basically booleans but used as integers (subtraction and multiplication)