CFrame.lookAt: how do I ignore the Y value?

A question asked all too many times, I fear.

local aimTweenInfo = TweenInfo.new(.1)
				local lookAt = CFrame.lookAt(AIRoot.Position, player.Character.HumanoidRootPart.Position)
				local aimTween = tweenService:Create(AIRoot, aimTweenInfo, {["CFrame"] = lookAt})
				aimTween:Play()
				aimTween.Completed:Wait()

This is fine, but if you jump, the enemy looks up at you, and by looks up, I mean rotates his body in ways that do not make sense to the laws of physics.
So basically, how do I keep the Y value constant, and make sure he only rotates his X and Z values?

Can you print the lookAt.Y? If so just set it as 0

If you want to keep a rotation axis constant you can do something like this

local constrainedLookAt = CFrame.fromMatrix(lookAt.Position, lookAt.XVector, lookAt.YVector)

this will remove rotation on the Z axis (up and down)
if you wanted to remove rotation on the X rotation axis then it would be something like

local constrainedLookAt = CFrame.fromMatrix(lookAt.Position, Vector3.new(), lookAt.YVector, lookAt.ZVector)

what does cframe.fromMatrix do?

It creates a CFrame (4x4 matrix) from 4 Vector3s (the last row of the matrix is (0, 0, 0, 1)), if you want to learn more about CFrames and matrices, you can look here

Also, how can I use this to eliminate the Y value?
It seems like it could be easy but I’m not sure and don’t wanna ruin it

So the example code I showed should do that.

lookAt = CFrame.fromMatrix(lookAt.Position, lookAt.XVector, lookAt.YVector)

The ‘pitch’ of the CFrame is its ZVector, which is filled in by the CFrame.fromMatrix constructor, eliminating any pitch from the cframe.

Well I tried that

local aimTweenInfo = TweenInfo.new(.1)
				local lookAt = CFrame.lookAt(AIRoot.Position, player.Character.HumanoidRootPart.Position)
				local constrainedLookAt = CFrame.fromMatrix(lookAt.Position, lookAt.XVector, lookAt.YVector)
				local aimTween = tweenService:Create(AIRoot, aimTweenInfo, {["CFrame"] = constrainedLookAt})
				aimTween:Play()
				aimTween.Completed:Wait()

unless i did something wrong, it didnt work

Can you try this

local constrainedLookAt = CFrame.fromMatrix(lookAt.Position, Vector3.new(), lookAt.YVector, lookAt.ZVector)

I probably have gotten my axes wrong, so maybe its the X axis.

I tried this and I do not know how nor do I know why but he just
he’s
he has
disappeared.
What the actual…?

This sounds hacky but you could do something like this:

local LookAtValue = CFrame.new(2,1,3)

LookAtValue *= CFrame.new(LookAtValue.X,LookAtValue.Y - LookAtValue.Y,LookAtValue.Z)

I don’t add stuff to CFrames often because it’s kinda a rare occurrence that you have to do that. So, this is probably entirely wrong…

If you want to remove the Y value of the orientation, then I think you would use: CFrame.fromEulerAnglesXYZ

local aimTweenInfo = TweenInfo.new(.1)
				local lookAt = CFrame.lookAt(AIRoot.Position, player.Character.HumanoidRootPart.Position)
                local target = CFrame.Angle(lookAt.X,0,lookAt.Z)
				local aimTween = tweenService:Create(AIRoot, aimTweenInfo, {["CFrame"] = target})
				aimTween:Play()
				aimTween.Completed:Wait()

So basically you create a target CFrame that uses the lookAt’s X and Z, and you get to ignore the Y

1 Like

Instead of 0 for the Y, you should probably get the part’s current Y value.

local constrainedLookAt = CFrame.fromMatrix(lookAt.Position, lookAt.XVector, AIRoot.CFrame.YVector)

This might work, I’m not sure

1 Like

He says he wants to ignore the Y value

I know this is an old post but you can do something like this

	local x = char.HumanoidRootPart.Position.X
	local y = touched.Parent.HumanoidRootPart.Position.Y
       local z = char.HumanoidRootPart.Position.Z
       							touched.Parent.HumanoidRootPart.CFrame = CFrame.lookAt(touched.Parent.HumanoidRootPart.Position, Vector3.new(x,y,z))

so you set the y value to the y value of the 1st object, it worked for me.