How do I make a camera follow a players x and z position but not Y

Hello. Im trying to make a game that’s a mix between devil may cry and Sekiro. Devil may cry combat is very fast paced and has a lot of vertical movement. Since the default camera always makes sure that the player is in the center of the screen, it might make the camera jitter up and down a lot.

The problem with this is that I don’t want to script an entirely new system. To me it seems like there is a simple solution like maybe creating a new camera subject that follows the players X,Z positions.

So far I have tried to create a new camera Cframe
that always updates to the players X,Z positions, however the camera decides to orient itself to 0,0,0 (I really don’t want to script a set orientation because it will make the camera janky).

Also, the camera needs to be controllable for this to feel good to use.

Sadly, I can’t send anything from my script currently since i’m writing this from my phone.

8 Likes

Forgive me if I misinterpreted your question but is this what you’re trying to do?
If so I can send it through!

2 Likes

Can’t you get the camera’s get the cameras z and x CFrame and use CFrame.new, putting the current x CFrame then 0 for Y and the current Z CFrame. You also should put it on runService.heartbeat

2 Likes

Yes, that is exactly what i meant

2 Likes

I reccomand RS.RenderSteps as it runs every frame

3 Likes
local Character = game.Players.LocalPlayer.Character


local RayParams = RaycastParams.new()
RayParams.FilterDescendantsInstances = Character:GetChildren() -- So the raycast doesn't hit any of the players body parts

game:GetService("RunService"):BindToRenderStep("FixCamera", Enum.RenderPriority.Last.Value + 1, function()
	local RaycastResult = workspace:Raycast(Character.PrimaryPart.Position, Vector3.new(0,-1,0) * 100, RayParams) -- Use a raycast to find the ground the player is standing on
	
	workspace.CurrentCamera.CFrame = CFrame.new(Vector3.new(
		workspace.CurrentCamera.CFrame.Position.X, 
		RaycastResult.Position.Y + 6,  -- "6" is height off the ground in studs, 6 looks natural to me but change it to your liking
		workspace.CurrentCamera.CFrame.Position.Z),  -- Camera position
		Vector3.new(Character.PrimaryPart.Position.X, RaycastResult.Position.Y + 6, Character.PrimaryPart.Position.Z)) -- LookAt
end)

This code finds the floor the player is on using a raycast and keeps the cameras Y axis offset 6 studs up from the floor. Casting a ray every frame can be expensive, so some work is needed on your part to only update the floors position when necessary!

1 Like

I should have probably been more specific. The camera should only do this when the player is doing an attack where the camera doesn’t need to follow them vertically.

3 Likes

You can bind the function to each RenderStep when the attack starts, and unbind it when the attack ends.

3 Likes

If the attack is in the air then i guess i just have change offset or is it something more difficult.

3 Likes

The code finds the floor below the player and offsets from the floor, so no changes should be needed.

2 Likes

Ok, i now noticed that you can’t move your camera vertically. That’s an issue

2 Likes

I have strung this together which works for the most part.

local function CameraSmoothing()
local x,y,z = Camera.CFrame:ToOrientation()

Camera.CFrame = CFrame.new(Camera.CFrame.Position.X, cameraCF.Position.Y, Camera.CFrame.Position.Z)
Camera.CFrame = CFrame.fromOrientation(x,y,z)+Camera.CFrame.Position

end

CameraCF is the Cameras Height when the move started

the only issue is that the camera always stays at the height of CameraCF when you move it

2 Likes

Okay! I have created a solution but i’ll have to post it when i’m on the computer

local function CameraSmoothing()
local x,y,z = Camera.CFrame:ToOrientation()
CameraStabilizing.CFrame = CFrame.new(Humrp.CFrame.X, Camera.Focus.Y,Humrp.CFrame.Z)
Camera.CameraSubject = CameraStabilizing
Camera.CFrame = CFrame.fromOrientation(x,y,z)+Camera.CFrame.Position
end

This is my solution. It allows full camera control and works seamlessly whilst most likely being less expensive than raycasting. It is easy to control and can be quickly adapted.

what it does is it has a object as a reference for where the camera should look (CameraStabilizing). This object tracks the players X,Z coordinates and takes the height of where the head was when the move started. Hopefully someone uses this instead of having to struggle.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.