Help finding an angle

I am trying to find the angle between two parts.

local target = workspace.Target
local part = workspace.Part
while task.wait(.01) do
	local angleA = target.CFrame
	local angleB = part.CFrame
	local finalAngle = angleB:toObjectSpace(angleA).LookVector.unit
	print(math.deg(math.asin(finalAngle.x)))
end
``` my current code does not seem to be working right, it always gives out numbers lower then 1

Not sure, but if you have the angle from finalAngle why are you doing math.asin on it? Try print(math.deg(finalAngle.x))

I’m pretty sure you need to use acos instead of asin.

Aside from that, what do you mean by “angle between two parts”. Do you mean, the angle facing that part? Or the angle between their rotations?

The LookVector is just a directional vector, its not the angle. Doing math.acos gets the angle of the vector.

1 Like

you could try target.orientation.X - part.orientation.X sine than it will calculate the angle diffrence between the 2 parts if u want part to look at the target you could do part.Cframe = CFrame.new(part.CFrame , target.CFrame)

only thing i would change is id use the both part’s positions instead of CFrame.
Then instead of using toObjectSpace id just do (partPos - targetPos) .Unit.

ps. if you’re trying to find the angle from the part to the target (part as the origin) then the equation becomes (targetPos - partPos).Unit

pps. You dont need to use .Unit on lookvector/rightvector/upvector because they are already a unit vector

This is the formula:

local function getAngle(a : Vector3, b : Vector3)
	return math.atan2(a:Cross(b).Magnitude, a:Dot(b))
end

Where a and b are the Vector3s of the respective parts.

1 Like

by “the Vector3s” do you mean the positions?

Yes, I mean the part’s position.

Oh, well in that case, how would i convert that returned value into degrees?

math.deg(radians)

example

math.deg(math.pi) --> 180
while task.wait() do
	local partA = target.CFrame
	local partB = part.CFrame
    local x = partA.Position.X
    local x2 = partB.Position.X
 
    local z = partA.Position.Z
    local z2 = partB.Position.Z

	print(math.deg(math.atan((x-x2) / (z-z2))))
end

maybe this could work?

target is the second part, [part] is the main part, i need to find the angle between [part] and target so i can usr motor on a hinge constraint, dont worry abt that for now tho. And I just started using cos, tan, sin, and their inverse functions not too long ago so im fairly new to the concept of trig in roblox studio.

1 Like

I can try that when im home (first period just started), but what’s the difference between acos and asin, and why would acos work for this?(no argument, just curious)

then again as my last reply to this post, whats the diff between acos, asin, atan and how to know to use which one where?

For a triangle with an angle θ , the functions are calculated this way:
https://www.mathsisfun.com/algebra/sohcahtoa.html

Sine: soh… s in( θ ) = o pposite / h ypotenuse
Cosine: …cah… c os( θ ) = a djacent / h ypotenuse
Tangent: …toa t an( θ ) = o pposite / a djacent
1 Like

basics im well aware of. My bad for being unclear on what i mean by that. What i meant is how to know which one to use in which situation?

You use the ones you have the information for.

You want to get an angle of a triange. So you need 2 sides of the triangle, any two sides you have the information for.

1 Like
local angle = math.acos(-(Part2.Position - Part1.Position).Unit.Z)

If you want it relative to Part1’s rotation, use this instead.

local angle = math.acos(Part1.CFrame.LookVector:Dot((Part2.Position - Part1.Position).Unit))
3 Likes

dont mind my 1 brain cell
what the heck does :Dot do