Make a player whos in first person face a certain direction?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

    SO! I have a game thats exclusively in first person and there are doors in my game that when interacted with teleport the player to the other side of the door (Like the doors from gasa4/doors in most rpgs) everything works perfectly however I’d like a way to make the player face a direction as their teleported so I can point them in the right direction!

  2. What is the issue? Include screenshots / videos if possible!

    Ive figured out that the player character can rotate just fine! But since the game is in first person the point your mouse is directed at always overrides any manual changes I make to the player. Changing the CFrame of the camera doesnt seem to help either

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

    I’ve tried many of the solutions here on the dev forum but nothing seems to work,
    finding the cameras CFrame and moving that, rotating the player character etc
    Maybe ive been doing it all wrong but any help would be greatly appreciated thanks!

(Im not asking anyone to write code for me unless your some kind of expert at camera stuff, just any functions/other scripting shenanigans that I could learn to help with my issue)

1 Like

You could try rotating the HumanoidRootPart CFrame when you teleport

As I said that doesnt work, the player is in first person the direction their mouse is pointing in overrides rotating the character. Thanks for the input tho!

Can you temporarily change the camera type to scriptable or something else right before the teleport, then teleport and then point the camera where you’re want to look, then change it back again? Maybe even wait a second or so after before setting the camera type back so they don’t quickly accidentally move the camera away from where you wanted right away? Or does scriptable not stop the mouse from camera movement in first person?

Are you using a custom first-person system?

I tested the following code in an empty baseplate and it worked fine in first-person:

--// On each render cycle following camera update we will set the camera CFrame to CFrame "lookat" part
game:GetService("RunService"):BindToRenderStep("Before camera", Enum.RenderPriority.Camera.Value + 1, function()
	workspace.CurrentCamera.CFrame = CFrame.new(workspace.CurrentCamera.CFrame.Position, workspace.Part.Position)
	
	--// Stop player from moving camera which causes jittering (hides mouse)
	game:GetService("UserInputService").MouseBehavior = Enum.MouseBehavior.Default
	game:GetService("UserInputService").MouseIconEnabled = false
end)

I’m gonna assume that when your player touches the door, they teleport to the other side. And you’re having trouble because after teleporting, the player is facing the direction they were initially looking at before touching the door.

Something like this:

What I assume you want to achieve is something like this:

From what I know, camera manipulation doesn’t work in server scripts and only in local scripts. (Correct me if I’m wrong). If it was only a single door, I would recommend making a local script. However, you have multiple doors. Local scripts still work but you would have to duplicate them and set their properties to their respective doors which is kind of tedious.

What I’d suggest is making a remote event under ReplicatedStorage. Use the CollectionService to tag your doors and give all your tagged doors the server script you’ve created for them. Make sure that upon interaction, the script uses :FireClient(player). If it’s a touch event, don’t forget to use :GetPlayerFromCharacter(). The script would look something like this:
(db = debounce, added to prevent spam teleportation)

local door = script.Parent
local db = false
local camera = workspace.CurrentCamera
local doorevent = game:WaitForChild("ReplicatedStorage").touchdoor

door.Touched:Connect(function(character)
	db = true
	local player = game.Players:GetPlayerFromCharacter(character.Parent)
	character.Parent:SetPrimaryPartCFrame(door.Parent.tp.CFrame + Vector3.new(0, 3, 0))
	doorevent:FireClient(player)
	task.wait(3)
	db = false
end)

(Vector3 is added to prevent the character from spawning inside the teleportation part)

As for the localscript to manipulate the camera, you can just add it under StarterPlayerCharacter. Simply just do .OnClientEvent:Connect(function()). Make sure to change the camera type to scriptable first, then change it back to custom after changing the angle.
Here’s an example:

local plrs = game:GetService("Players")
local plr = plrs.LocalPlayer
local cam = workspace.CurrentCamera

local rs = game:GetService("ReplicatedStorage")
local doorevent = rs:WaitForChild("touchdoor")

doorevent.OnClientEvent:Connect(function()
	cam.CameraType = Enum.CameraType.Scriptable
	cam.CFrame = CFrame.new(cam.CFrame.Position) * CFrame.Angles(0, math.rad(90), 0)
	cam.CameraType = Enum.CameraType.Custom
end)

If your door is using a proximity prompt instead, the prompt will immediately get your player instead of your character, so you wouldn’t need to use :GetPlayerFromCharacter(). Just simply edit character:SetPrimaryPartCFrame to player.Character:SetPrimaryPartCFrame.
Here’s what it would look like:

local door = script.Parent
local pp = door.ProximityPrompt
local db = false
local camera = workspace.CurrentCamera
local doorevent = game:WaitForChild("ReplicatedStorage").touchdoor

pp.Triggered:Connect(function(player)
	db = true
	player.Character:SetPrimaryPartCFrame(door.Parent.tp.CFrame + Vector3.new(0, 3, 0))
	doorevent:FireClient(player)
	task.wait(3)
	db = false
end)
5 Likes

PS: If you have a specific direction you want your player to look at and not an angle, you’re gonna want to make a transparent part, set its position at the direction you want, and reference it in the script. Then you’re gonna replace CFrame.Angles() with LookPart.CFrame.Position.
Here’s what it looks like:

Local LookPart = workspace.Part
-- Other code here

cam.CFrame = CFrame.new(cam.CFrame.Position, LookPart.CFrame.Position)
1 Like

A simple way to force a player to look in a certain direction is to set the cameras cframe

workspace.CurrentCamera.CFrame = CFrame.lookAt(workspace.CurrentCamera.CFrame.Position, target.Position)

This will force the camera to face towards the target

1 Like

THANK YOU SO MUCH!! This is exactly the solution i was looking for!
I cant believe you took the time and effort to code and try it yourself just for my stupid little question tysm!

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