How to ignore x and z of CFrame.LookAt

I want x and z to be ignored. But it is unclear how to do it, since there is no solution here about .

while wait() do
	local sourcePosition = lookPart.Position
	local targetPosition = part.Position

	lookPart.CFrame = CFrame.new(sourcePosition,Vector3.new(sourcePosition.X, targetPosition.Y, sourcePosition.Z))
end

At the same time, it is necessary that x and z do not change, but only y changes depending on how high the target is. But the principle CFrame.LookAt must be maintained, so that part takes aim like a cannon (like Angry Birds).

1 Like

The given positions could have a different y position. This means that it’ll affect the x and or z axis when rotating.

Firstly, let’s define the positions:

local sourcePosition = part1.Position
local targetPosition = part2.Position

These two variables are the positions of both parts.


Now let’s construct a new vector based on the two positions. This vector is a direct copy of the ‘targetPosition’ variable, with the only difference being that the y axis is replaced with the ‘sourcePosition’ y axis:

local targetVector = Vector3.new(targetPosition.X, sourcePosition.Y, targetPosition.Z)

Now we can construct a new cframe given our ‘sourcePosition’ and newly made ‘targetVector’ variable:

local cframe = CFrame.lookAt(sourcePosition, targetVector)

And this is the final product:

-- variables
local part1 = workspace.a
local part2 = workspace.b

-- functions
while true do
	local sourcePosition = part1.Position
	local targetPosition = part2.Position
	
	local targetVector = Vector3.new(targetPosition.X, sourcePosition.Y, targetPosition.Z)
	
	local cframe = CFrame.lookAt(sourcePosition, targetVector)
	
	part1.CFrame = cframe
	
	task.wait()
end

After checking your code, it works as usual, turns the part and does not even turn the part up or down. And I need him to turn the part up or down, and not take into account x and z.

I don’t understand what you’re trying to imply; draw an example, or illustrate via a gif.

There should be a turn only up and down depending on the position of the target.

I understand now. Instead of manipulating the y axis, try the x instead:

local targetVector = Vector3.new(sourcePosition.X, targetPosition.Y, targetPosition.Z)

For any future viewers, this translates to:

2 Likes

It worked out. Thank you!!!

1 Like

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