How to make a part face the direction of another part

How do I make a part face the direction of another part. I tried several things like getting the magnitude and trying to use CFrame.Angles but I simply cant get it to work please help.

6 Likes

Use CFrame.lookVector; it can be used any time a CFrame is present (for instance, checking the lookVector of a part’s CFrame to get where it’s facing)
CFrame docs

2 Likes

Yeah I’m new to lookvector idk how to do that

If I’m correct I believe you can just do

local part1 = workspace.Part1
local part2 = workspace.Part2
local pos = CFrame.new(part1.Position, part2.Position)
part1.CFrame = pos

Which means you didn’t actually need lookvector in the end and I was overthinking it.

7 Likes

Hmmm that doesnt seem to work here is my script

local part1 = script.Parent

local part2 = workspace.Parent

local pos = CFrame.new(part1.Position, part2.Position)

while true do

wait()

part1.CFrame = pos

end
1 Like

Position is not a valid member of DataModel

I didn’t know you wanted to constantly make it look towards the part. If you want to replicate this locally, use RunService.RenderStepped as it will be smoother.

2 Likes

Yes but how do i make it face the other part I have tried so many things

For the second part, you used workspace.Parent, which isn’t a part, but it’s actually the instance game.

5 Likes

Oops that was an autosuggestion mistake lol.

2 Likes

Cframe.new(position, lookat) has been deprecated.

Consider using CFrame.lookat(position, lookat)

12 Likes

Is there any way to make it so that it only rotates on its Y axis?

First time posting ever (just discovered I can do this) so sorry if im doing something wrong.

What do you mean

I am assuming you mean like your game/experience/place whatever is top down or something and height doesn’t matter, and you want a part to face another part. So, I suppose you first find out how far away the part to look at is (in 2D)

local hype = math.sqrt((firstpart.Position.X - lookpart.Position.X)^2 + (firstpart.Position.Z - lookpart.Position.Z)^2) -- hypotenuse 

-- Me was idiot as I wrote Y instead of Z

Then you find out how far one dimention distance is, like you know how there is like x line and y line so basically choose x or y and find difference, I choose x because habit

local sad = firstpart.Position.X - lookpart.Position.X -- side

Since you now have a hypotenuse and a side, you can now do trigeometry. There are 3 angle in a triangle (duh, its called a TRI angle) we have right angle (obviously wrong option) and two other angles. So basically try all 2 of the options, one of them it should work

local angle = math.asin(sad/hyp) -- angle option 1
-- or try this
local angle = math.acos(sad/hyp) -- angle option 2

These give out angle in radical, so then you use

angle = math.deg(angle)

to get degrees, then you put it in the part, you may need to add a angle like 90 or something

lookpart.Orientation = vector3.new(lookpart.Orientation.X,angle,lookpart.Orientation.Z)

I never tested my solution, so tell me if it dosen’t work, and I will try to fix. I could also try to explain trigeometry better if you want.

Edit:

local lookpart = game.Workspace.lookpart
local firstpart = game.Workspace.Targetpart

while true do
	local hyp = math.sqrt((firstpart.Position.X - lookpart.Position.X)^2 + (firstpart.Position.Z - lookpart.Position.Z)^2) -- hypotenuse 
	local sad = firstpart.Position.X - lookpart.Position.X -- side
	local angle = math.asin(sad/hyp) -- angle option 1

	angle = math.deg(angle)
	if firstpart.Position.X - lookpart.Position.X < 0 then
		angle = 180-angle
	elseif firstpart.Position.Z - lookpart.Position.Z<0 then
		angle = 180-angle
	end
	if firstpart.Position.X - lookpart.Position.X < 0 and firstpart.Position.Z - lookpart.Position.Z>0 then
		angle = 360-(angle-180)
	end
	print(angle)
	wait(0.1)
end

This should print out the angle the part is supposed to rotate.

Oh haha, Seems like I’ve already found out the solution, seems like you’ve overcomplicated it

Essentially, you create new coordinates for part1 and part2 where both of them are seemingly on the same Y axis

part1.Position = Vector3.new(part1.Position.X, 0, part1.Position.Z)
part2.Position = Vector3.new(part1.Position.Z, 0, part2.Position.Z)

Then using that, I calculate the lookat

local pos = CFrame.new(part1.Position, part2.Position)

Virtually, part1 and part2 are on the same Y axis meaning that it will only rotate on its’ Y axis

Also, I’ve been told it’s better to use CFrame.lookat() rather than CFrame.new()

local rotation = CFrame.lookAt(Position1, Position2)
humanoidRootPart.CFrame = rotation