How to make a character face a specific direction?

Hey, so, I’m using moveto() for the player and NPCs, and I was wondering how I can make them face a specific direction after being moved to?

I did some research and found this to add into my function:

function onTouched(hit)  
  local human = hit.Parent 
local rotationCFrame = CFrame.Angles(0, math.rad(180), 0)
  if human then
human:SetPrimaryPartCFrame(human:GetPrimaryPartCFrame() * rotationCFrame)
script.Parent.Touched:connect(onTouched)

Now, I don’t recommend doing this, because in order for this to work the NPC/player has to be facing the exact opposite directon.

It’s hard to explain what I’m saying but picture this.

Imagine you want your player facing the backwards direction (-z)
And your player is facing the forwards direction (+z)

If your player is facing +z, the code will work as intended. But, the story changes if your player is facing the left (-x) direction. In that case, the code will make your player face the right direction (+x). However, my goal, is to make the player face the backwards direction (-z) regardless of where they were originally facing/coming from.

So, what can I do to achieve this?

1 Like
local pos = --a vector3 describing position
local lookAt = --a vector3 describing where the object should look

local cf = CFrame.lookAt(pos, lookAt)
--use cf to set the character CFrame
1 Like

they asked for a way for an NPC to face a direction, not a position.

you can just set the CFrame’s position as the NPC’s position and then multiply it by the desired rotation this way:

local NPCPosition = (YOUR NPCs CURRENT POSITION)
local DesiredRotation = Vector3.new(0, math.rad(180), 0) -- change this to whatever you want, make sure each number is inside its own math.rad()

local Result = CFrame.new(NPCPosition) * CFrame.Angles(DesiredRotation) -- this is your new CFrame

by the way, i would recommend you using PivotTo instead of SetPrimaryPartCFrame since that’s not deprecated and it doesn’t require a PrimaryPart.

2 Likes

You can use CFrame.fromMatrix()

What this do is creating a CFrame facing a specific direction. Because this uses directions, you will use Vector3s.

Sample code:

local XVector = Vector3.new(-1, 0, 0) -- direction to the left (-x)
local YVector = Vector3.new(0, 1, 0) -- direction to the up (+y)
local ZVector = Vector3.new(0, 0, -1) -- direction to the front (-z or +lookVector)

local CFrameFacingBackwards = CFrame.fromMatrix(Vector3.new(0, 0, 0), XVector, YVector, ZVector)

The code above creates a CFrame in the position 0, 0, 0 and facing backwards (-lookVector).

3 Likes

The OP said that she doesn’t recommend this because the character needs to be facing exactly the opposite direction of the desired direction

She wants that even if a character is facing left, right, or any direction, she can make it face EXACTLY south.

you did not get their problem, they stated that their code would only “work” (work as in work in the desired way) if the player was facing the opposite direction that the scripted wanted them to face.

the sample code that they found from another post did that, it just added 180 degrees to the rotation in the y direction. the code that i gave them gets the player’s position and adds the desired rotation on top, not just the position and the desired rotation gets added on top of its rotation.

but it would be hard to get the angle needed to face EXACTLY the desired direction

with CFrame.fromMatrix(), you can create a CFrame facing the EXACT direction by specifying the direction of the desired right direction (RightVector or XVector), up direction (UpVector or YVector), and (optional) back direction (-LookVector or ZVector)

p.s. i tested my solution in studio on a part and it faced exactly south

Sorry for the delayed response, I have been attempting to integrate your code into my function for a while and it’s not producing desired results. In some cases the avatar flings around. I’m starting to think it’s from using MoveTo(). Can you add it to my function?

I’m using MoveTo() without referencing Humanoid to imitate teleportation.

Full function:


local XVector = Vector3.new(-1, 0, 0) -- direction to the left (-x)
local YVector = Vector3.new(0, 1, 0) -- direction to the up (+y)
local ZVector = Vector3.new(0, 0, -1) -- direction to the front (-z or +lookVector)

local CFrameFacingBackwards = CFrame.fromMatrix(Vector3.new(0, 0, 0), XVector, YVector, ZVector)

function onTouched(hit)  
  local human = hit.Parent
 if human then
	human:MoveTo(game.Workspace.Ends.End1.Position)
-- Can you incorporate the matrix into the rest of the  function here?--

script.Parent.Touched:connect(onTouched)

Also for anyone still confused about what I want to achieve:

try using PivotTo()

example:

local PartToLookAt = workspace.Part
local TeleportPosition = workspace.Part2.Position
local CFrameTarget = CFrame.new(TeleportPosition,PartToLookAt.Position)

Character:PivotTo(CFrameTarget)
1 Like

You simply use PivotTo()

local XVector = Vector3.new(-1, 0, 0)
local YVector = Vector3.new(0, 1, 0)
local ZVector = Vector3.new(0, 0, -1)

function onTouched(hit)  
	local human = hit.Parent
	if human then
		human:PivotTo(CFrame.fromMatrix(workspace.Ends.End1.Position, XVector, YVector, ZVector))
	end
end

script.Parent.Touched:Connect(onTouched)
2 Likes

Thank you, works as intended, wishing you a great day.

1 Like

can you mark my first reply the solution please? it makes more sense and it will show the more understandable solution to the future readers. thanks

1 Like

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