I just wanted to know how to make a Part face a Player's Torso. And can you explain how it works too?
Thanks in advance!
Hey MBbraveDragon,
part.CFrame = CFrame.new(position, lookAt);
-- Torso and Part defined above as 'torso' and 'part'. part.CFrame = CFrame.new(part.Position, torso.Position);
-- Torso and Part defined above as 'torso' and 'part'. part.CFrame = CFrame.new(part.Position, Vector3.new(torso.Position.X, part.Position.Y, torso.Position.Z));
~~ KingLoneCat
The most common way in Roblox is to use a CFrame since it can calculate the rotation for us though there is nothing stopping you from creating your own function to calculate the angles with trigonometry.
It is important to look at the constructors for a Roblox data constructor as you should use the correct one depending upon your needs. In your case we would use CFrame.new(Vector3 pos, Vector3 lookAt)
desc:- "Creates a CFrame positioned at pos looking at the lookAt position." the most important part is the "lookAt" position as this Vector3 is what will be used internally to calculate the rotation to look at that given Vector3 point.
Side note:- CFrame rotation is in radians not in degrees
A simple example:-
-- setup the part local part = Instance.new('Part') part.CFrame = CFrame.new(0, 5, 0) part.Anchored = true part.Parent = workspace local partPosition = part.CFrame.p -- the vector 3 position as it will not change local torso game.Players.PlayerAdded:Connect(function(plr) plr.CharacterAdded:Connect(function(charModel) torso = charModel:WaitForChild('Torso') -- store the torso in a variable end) end) -- a small loop while true do wait() if torso then -- if we have a torso ie not nil -- usin the constructor CFrame.new(position, lookAt) part.CFrame = CFrame.new(partPosition, torso.Position) end end
I hope this helps, please comment if you do not understand how / why this code works.