How would I make the player face forward (relative to the camera) when pressing a button

I wish to make the player face the direction that the camera when they click Z, but I don’t want that direction to cause them to look at the ground or at the sky if the camera is pointing there. I just want it to use the camera’s X and Z.

1 Like

you mean like this?

local function faceCamera()
	local player = game.Players.LocalPlayer
	local character = workspace:FindFirstChild(player.Name).HumanoidRootPart
	local characterPos = character.CFrame.Position
	local cameraPos = workspace.CurrentCamera.CFrame.Position
	local newCameraPos = Vector3.new(cameraPos.X,characterPos.Y,cameraPos.Z)
	local lookAtPos = newCameraPos:Lerp(characterPos,2)

	local cframe = CFrame.new(characterPos,lookAtPos) 

	character.CFrame = cframe 
end
3 Likes

If I’m reading this correctly, you want the player to face the same direction as the part and not AT the part, right? It sounds like you specifically need help with your character not falling over because they are looking in the Y direction, which rotates their character and makes them fall over.

My solution is a bit janky but it works. Add an attachment to the part of your camera, and then move it far in the distance (like 1000 studs out in front of the way the camera is facing). This is how you could use CFrame.new(Position, LookAt) as a solution for this.
After that, try adding this line to your code:

HumanoidRootPart.CFrame = CFrame.new(HumanoidRootPart.Position, Vector3.new(CameraPart.Attachment.WorldPosition.X,HumanoidRootPart.Position.Y,CameraPart.Attachment.WorldPosition.Z))

The X and Z in the LookAt vector (the 2nd argument in CFrame.new) are the attachment’s world positions, but the Y in the LookAt vector is the elevation the HumandRootPart is currently in, so it doesn’t have to tilt over to look in the same direction.

Hopefully someone gives a more legit answer, I’m sure mine isn’t the most optimal.

2 Likes

This works well, the only thing is the player doesn’t gradually rotate, its more of a snap, so maybe the lerp isn’t working correctly im not sure.

1 Like

you can use TweenService for that

local tweenTime = .2 -- change this
local tween:Tween = false

local function faceCamera()
	local player = game.Players.LocalPlayer
	local character = workspace:FindFirstChild(player.Name).HumanoidRootPart
	local characterPos = character.CFrame.Position
	local cameraPos = workspace.CurrentCamera.CFrame.Position
	local newCameraPos = Vector3.new(cameraPos.X,characterPos.Y,cameraPos.Z)
	local lookAtPos = newCameraPos:Lerp(characterPos,2)
	local cframe = CFrame.lookAt(characterPos,lookAtPos) 
	
	local tweenService = game:GetService("TweenService")
	local info = TweenInfo.new(tweenTime)
	
	if (tween and tween.PlaybackState == Enum.PlaybackState.Playing) == false 
	and character.Parent.Humanoid:GetState() == Enum.HumanoidStateType.Running
	and character.Parent.Humanoid.MoveDirection == Vector3.new(0,0,0) then
		tween = tweenService:Create(character,info,{CFrame = cframe})
		tween:Play()
	end
end

i needed to stop the tween from playing if the player is moving or jumping. does that matter for you?

1 Like

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