I'm struggling with CFrame!

Hi! I want to have the player look in the same direction as a part, whilst still maintaining their position and UpVector. I want the player to be looking in the same direction as a part not looking at the part. I tried a lot of things, putting in a lot of numbers but I can never get it to work. I’ve tried using CFrame.LookAt and it’s just not working. What’s an easy or good way to position this?

I want the player to be like this in relation to the part.

Every time I try it, the player always ends up like this.

I’m doing this in a wall jump which changes the humanoid root parts velocity to spring towards the other side which all works fine, but the player is never position correctly in relation to the part.

It’s one line of code that I cannot figure out.

You could change the HumanoidRootPart’s Orientation value to the Part’s Orientation, skipping the X axis.

HumanoidRootPart.Orientation.Y = Part.Orientation.Y
HumanoidRootPart.Orientation.Z = Part.Orientation.Z

Pretty sure it rotates the rest of the character aswell.
Havent tested this though, so im not sure if it will work how you wanted it to

Apparently there is a second parameter on CFrame.new() which allows the CFrame created to face the other position. Both arguments are Vector3.

HumanoidRootPart.CFrame = CFrame.new(HumanoidRootPart.CFrame.Position, TargetPart.Position)

However, this doesn’t work properly, if the said part is backwards for whatever reason. In this case, you would try to use the negative position instead.

Okay that doesn’t really work. What I mean is I want the UpVector to be the same but it’s not working in lookAt I think? I’m not very good with CFrame.

Do you want the body to rotate towards the part?

It has the character look towards the middle of the part though?

I want the character to not be looking at the part but the opposite direction. I want it to be looking the same direction as the part.

Im assuming you only want the character to turn left and right, otherwise it would fall if the part rotates in any other axis

You want to keep the X orientation? Correct me if I’m wrong but I think you mean the Y orientation as Y is up and down.

local Orientation = Part.Orientation

Character:SetPrimaryPartCFrame(CFrame.new(Character.PrimaryPart.Position) * CFrame.fromEulerAnglesXYZ(Character.PrimaryPart.Orientation.X, Orientation.Y, Orientation.Z))

If it is Y, you can always switch the X and Y orientation between the character and the part. :slight_smile:

I want to keep the UpVector, I don’t know why I wrote it like that.

Yes…? I want the character’s humanoid root part to be parallel with the part.

The only step remaining is to ensure that the CFrame.fromOrientation(0, HumanoidRootPart.Orientation.Y, HumanoidRootPart.Orientation.Z). Pretty sure it works?

It is still confusing how this should be aligned. Because I’m lacking sufficient information to actually visualize this correctly as you put it.

Then you will need to switch the X and Y with the answer I gave above.

local Orientation = Part.Orientation

Character:SetPrimaryPartCFrame(CFrame.new(Character.PrimaryPart.Position) * CFrame.fromEulerAnglesXYZ(Orientation.X, Character.PrimaryPart.Orientation.Y, Orientation.Z))

Some vector math and CFrame.fromMatrix will work for this:

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local root = character:WaitForChild("HumanoidRootPart")

local part = workspace.Part -- or whatever

game:GetService("RunService").Stepped:Connect(function(t, dt)
	local up = root.CFrame.UpVector -- could also set this to a constant Vector3.new(0,1,0)
	local almostBack = (root.Position - part.Position).Unit
	local right = up:Cross(almostBack)

	root.CFrame = CFrame.fromMatrix(root.Position, right, up)
end)
4 Likes

This almost works, but is there anyway to make it more parallel to the part? It’s always at a slight angle.

I can’t really use orientation because it’s sometimes different. How can I do it not using orientation?

Is it possible if you can screenshot or depict anything of it for visual purposes?

What do you mean you can’t use orientation because it’s sometimes different?

Not sure what you mean by “parallel to the part”, it’s working as expected for me.

I changed the post with more information.