How do I make a player face a certain part?

So I have a building, which I want people to be able to climb. How would I make them face the building. The player orientation has to be set so its facing the building, but I am not sure how to do that. Any help is appreciated.

7 Likes

alright test successful
you just need to set the PrimaryPartCFrame of the player’s character like this

local destinationPart = {part you want the player to face ex: red part}

local placementpart = {a random part, set in the position and direction you want the player to face ex: green part}

script.Parent.clickpart.ClickDetector.MouseClick:Connect(function(player) {or however you will activate it}

local playerChar = game.Workspace[player.Name] {get the players character in the workspace}

playerChar:SetPrimaryPartCFrame(placementpart.CFrame) {this will set the players position and orientation to that of the block ex:green}

end)

7 Likes

(Someone already replied, but I might as well provide an answer anyway)

You’re going to want to change the character’s PrimaryPart (typically HumanoidRootPart)'s CFrame to keep the same position, but change the orientation. Fortunately, instead of doing a bunch of math, we can take advantage of this CFrame constructor:

CFrame.new(Vector3 position, Vector3 lookAt)

So here’s a simple example of facing a character towards your target brick

function lookAt(chr,target) --assume chr is a character and target is a brick to look towards
	if chr.PrimaryPart then --just make sure the character's HRP has loaded
		local chrPos=chr.PrimaryPart.Position --get the position of the HRP
		local tPos=target.Position --get the position of the target
		local newCF=CFrame.new(chrPos,tPos) --create our CFrame
		chr:SetPrimaryPartCFrame(newCF) --set the HRP's CFrame to our result, thus moving the character!
	end
end

It’s worth noting that if the target is significantly above/below the character, it may appear tilted for a brief moment before the character automatically rights itself. However, we can manage to avoid that by simply only using the X and Z axis of the target position, replacing the Y axis with that of the character position:

function lookAt(chr,target) --assume chr is a character and target is a brick to look towards
	if chr.PrimaryPart then --just make sure the character's HRP has loaded
		local chrPos=chr.PrimaryPart.Position --get the position of the HRP
		local tPos=target.Position --get the position of the target
		local modTPos=Vector3.new(tPos.X,chrPos.Y,tPos.X) --make a position at the target, but with the height of the character
		local newCF=CFrame.new(chrPos,modTPos) --create our CFrame
		chr:SetPrimaryPartCFrame(newCF) --set the HRP's CFrame to our result, thus moving the character!
	end
end

I hope this helped!

23 Likes

EDIT: I’ve noticed this old reply is still being viewed so I’ve updated it to include some new APIs.


Well there are two ways to make a player face a direction, CFraming or using AlignOrientation

  • You may want to refrain from CFrame can cause non-smooth movement, constantly CFraming a character causes issues where your controls feel like they are ignored.

  • Using a AlignOrientation would be best, using physics to set the position & face-direction allows smoother & nicer character movement.
    Set the mode to OneAttachment, then you can set AlignOrientation.CFrame.


Now here’s the reason why my reply is awfuly long, you can’t just have the player face towards the center of the building, this is incorrect, instead you need them to be aligned with the wall. The easiest way this can be done is with raycasting.

Wrong
CenterOrientation
Correct
RayOrientation

So how do I do this? Using workspace:Raycast() it is possible to get the normal (surface direction) easily, I’ll provide some steps:

First, we want our raycast to only hit our target building, so we’ll create a RaycastParams whitelist.

local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Whitelist
raycastParams.FilterDescendantsInstances = {BUILDING}

Next, we’ll fire a raycast from the character to the building’s position (center).

local origin = character.HumanoidRootPart.Position
local direction = BUILDING.Position - origin
local raycastResult = workspace:Raycast(origin, direction, raycastParams)

Now using the raycast result, we can modify our AlignOrientation to make a character face the wall.

if raycastResult then -- Did hit?
	alignOrientation.CFrame = CFrame.new(Vector3.zero, -raycastResult.Normal)
end

Demo Script

Put script in StarterPlayerScripts, add a part under workspace named “Building”

-- Demo of aligning character to a part's surface normal.
-- Greenboo5

local player = game.Players.LocalPlayer
local BUILDING = workspace:WaitForChild("Building")

-- Step 1: Params whitelist.
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Whitelist
raycastParams.FilterDescendantsInstances = {BUILDING}


player.CharacterAdded:Connect(function(character)
	local humRootPart = character:WaitForChild("HumanoidRootPart")
	
	-- Create alignOrientation.
	local alignOrientation = Instance.new("AlignOrientation", humRootPart)
	alignOrientation.MaxTorque = 100000000
	alignOrientation.Mode = Enum.OrientationAlignmentMode.OneAttachment
	alignOrientation.Attachment0 = Instance.new("Attachment", humRootPart)
	
	
	local stepped = game:GetService("RunService").Stepped:Connect(function()
		-- Step 2: Raycast from character to building.
		local origin = humRootPart.Position
		local direction = BUILDING.Position - origin
		local raycastResult = workspace:Raycast(origin, direction, raycastParams)
		
		-- Step 3: Using the hit normal.
		if raycastResult then -- Did hit?
			alignOrientation.CFrame = CFrame.new(Vector3.zero, -raycastResult.Normal)
		end
	end)
	
	
	player.CharacterRemoving:Wait()
	stepped:Disconnect()
end)
32 Likes