How to make simple leaning?

I have been trying to make a script for rotating the camera for a while now to no avail, I could just use camera offset but I would like for the camera to tilt too.

The camera simply doesnt budge on the z axis, nothing seems to work other than using the scriptable type which doesn’t follow the head.

Anything to help is greatly appreciated, thanks.

1 Like

Hello, thanks for posting!

I do remember seeing a video on this a while and I remember it being rather useful! It works in 3rd and 1st person with editable values.

Its not made by me but its a great tutorial.

Check it out here I hope it helps! :grin:

I am aware about positionally leaning but I would like to have rotation too, that’s where everything goes wrong. Thanks for the quick reply anyway. :slight_smile:

One of these threads may be of assistance.

1 Like

Seems to work but not, it wants to rotate but it fights back and doesn’t quite do it, it rotates like 1 degrees and pushes back, if i make it rotate 100 then it is quite visibly shaking.

I saw another script which puts the camera into scriptable mode and then rotates it and stops the player from moving until released so it makes me wonder if its possible… But I have seen games do it before so my brain hurts.

Would you mind clarifying exactly what it is you’re looking to achieve? The original post was fairly vague so I could only provide what has worked for others before, examples in the form of images, videos etc. would be helpful.

The ability to press e or q to lean in a direction moving the camera and rotating the camera.
I know how to move it but not rotate it.

Search the forum for similar threads and if you find any code that works hook it to the UserInputService's ‘InputBegan’ event/signal for the ‘E’ and ‘Q’ keys.

I already have done before making this post.

There’s plenty of ways to achieve this effect.
A way that I can think of, off the top of my head, is by using ContextActionService to bind your Q and E key to a function that handles the leaning itself. Then, you could bind a function to RenderStep that tilts your camera’s Z axis (ex. CurrentCamera.CFrame *= CFrame.Angles(0, 0, math.rad(LeanAngle))), and your character aswell.

I’d suggest reading up on ways to manipulate the character’s RootJoint C0/C1 to achieve the ‘character rotation’ effect you would like.

2 Likes

Would you be able to specify how many studs and degrees the camera should move and rotate respectively in response to the ‘Q’ and ‘E’ keys being pressed?

I already know about how to get input in fact I already have code to do the positional stuff, and I tried that too

Ill prob need to actually see it to calibrate it to my needs ig…

Please show your code if you’ve already implemented this and just need help with fixing the leaning in terms of positioning.

contextActionService:BindAction("leanLeft", function(name, state)
	if state == Enum.UserInputState.Begin then
		lean = -2
	else
		lean = 0
	end
end, true, Enum.KeyCode.Q, Enum.KeyCode.DPadLeft)
contextActionService:SetTitle("leanLeft", "Lean left")

contextActionService:BindAction("leanRight", function(name, state)
	if state == Enum.UserInputState.Begin then
		lean = 2
	else
		lean = 0
	end
end, true, Enum.KeyCode.E, Enum.KeyCode.DPadRight)
contextActionService:SetTitle("leanRight", "Lean right")

I have a renderstepped to move the camera as other code also moves it so its better placed there.

I’m sorry but are you doubting me?

This sounds like you need assistance with your code.

I just found something that worked, a little bit on my code was resetting the rotation from an old attempt i forgot to remove. Sorry.

local Enumeration = Enum
local Game = game
local Workspace = workspace
local RunService = Game:GetService("RunService")
local UserInputService = Game:GetService("UserInputService")
local Camera = Workspace.CurrentCamera

local Connection

local function OnRenderStep(Input)
	Camera.CFrame *= CFrame.new(if Input == "Q" then -0.5 elseif Input == "E" then 0.5 else 0, 0, 0) * CFrame.Angles(0, 0, if Input == "Q" then math.pi / 32 elseif Input == "E" then -math.pi / 32 else 0)
end

local function OnInputBegan(InputObject, GameProcessed)
	if GameProcessed then return end
	local Input = InputObject.KeyCode.Name
	if not (Input == "Q" or Input == "E") then return end
	if Connection and Connection.Connected then return end
	Connection = RunService.RenderStepped:Connect(function() OnRenderStep(Input) end)
end

local function OnInputEnded(InputObject, GameProcessed)
	if GameProcessed then return end
	local Input = InputObject.KeyCode.Name
	if not (Input == "Q" or Input == "E") then return end
	if Connection then Connection:Disconnect() end
end

UserInputService.InputBegan:Connect(OnInputBegan)
UserInputService.InputEnded:Connect(OnInputEnded)

This is likely more inline with what you were trying to achieve, tweens could be implemented into this script if necessary.

3 Likes

I had already done it but thanks, also I don’t think your supposed to send a full script.