Is there a way for CFrame to not affect Y axis?

I’m making a sort of shift lock which is making the CFrame of a player’s humanoid root part point to the mouse.Hit.p
Problem is, It also affects Y axis which is orientating the players up and down, that’s really glitchy.
I didn’t find anything on the internet regarding to this specific problem.

This is my current code:

plr.Character.HumanoidRootPart.CFrame = CFrame.new(plr.Character.HumanoidRootPart.Position,hitp)

The best way to do that is to make hitp.Y the same as the humanoid root part’s Y:

plr.Character.HumanoidRootPart.CFrame = CFrame.lookAt(
	plr.Character.HumanoidRootPart.Position,
	Vector3.new(hitp.X, plr.Character.HumanoidRootPart.Position.Y, hitp.Z)
)

If you know anything about the CFrame.lookAt, this is self-explanatory.

The only problem you could be facing with this is also shift-locking any vehicles you sit in, which can be fixed by not setting CFrame if the character’s humanoid is seated.

You can also simplify this code by storing plr.Character.HumanoidRootPart in a variable:

local root = plr.Character.HumanoidRootPart

root.CFrame = CFrame.lookAt(
	root.Position,
	Vector3.new(hitp.X, root.Position.Y, hitp.Z)
)
2 Likes

Thanks, it works. Only problem is that it’s preventing a player from walking.

Going off of @goldenstein64’s idea you may want to put a BodyGyro in the character’s HumanoidRootPart, and update the BodyGyro.CFrame instead of the HumanoidRootPart.CFrame to allow the player to walk.

2 Likes

Oh, good one. Haven’t thought about that.
It works, thanks