So I want to rotate a part towards another part (Using CFrame), but only by a certain degree, not fully rotate it. I don’t really know how to do this, and I haven’t seen similar posts (or I was too lazy to find them)
What do you mean by “a certain degree?” Like 10, 15, or 45 degrees? And do you want it to be an instantaneous rotation or a gradual rotation?
If you only need to look a percentage of the way towards an object, you should use CFrame:Lerp(). If not, see below:
I’m a little rusty on my trigonometry, but this should be what you are looking for.
This should give you the angle in degrees between two parts, you can then do whatever you need to do with the resulting value.
local partA = workspace.Part
local partB = workspace.Part2
while true do
task.wait()
-- Calculate the side of our triangle
local side = partA.Position.X - partB.Position.X
-- Use pythagorean theorem to get the hypotenuse
local hypotenuse = math.sqrt((partA.Position.X - partB.Position.X)^2 + (partA.Position.Z - partB.Position.Z)^2)
-- Calculate the angle
local angle = math.asin(side/hypotenuse)
-- Subtract that from the current parts rotation
local difference = math.rad(angle) - partB.Rotation.Y
-- Now you have your difference, and can do whatever with it.
print (math.deg(difference))
end
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.