Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to only change Y with CFrame.lookAt?

Asked by
Exarpo -3
3 years ago
Edited 3 years ago

So this is a piece of code I have, It makes a model look in the direction fo something, but what if I only wanted to change the Y value and not the X and Z? I'm using .lookAt and it fills in all the values (XYZ), but I only want to use the Y and keep the X and Z the same.

script.Parent.PrimaryPart = script.Parent:FindFirstChildWhichIsA("Part") script.Parent:SetPrimaryPartCFrame(CFrame.lookAt(script.Parent.PrimaryPart.CFrame.Position, closest.Position))

1 answer

Log in to vote
0
Answered by
Luathron 185
3 years ago
Edited 3 years ago

You should use CFrame.fromMatrix, where you have control over the rightvector, upvector, and lookvector. I assume you wanted to change just the Y rotation, so heres the code for it

local pos = script.Parent.PrimaryPart.CFrame.Position
local up = Vector3.new(0, 1, 0)
local forward = closest.Position - pos
local r = forward:Cross(up) -- constructing the rightvector
script.Parent:SetPrimaryPartCFrame(CFrame.fromMatrix(pos, r.Unit, up)) -- CFrame.fromMatrix will automatically calculate the lookvector from the rightvector and upvector
Ad

Answer this question