Trigonometry Issue

Hi everyone, I’m trying to calculate the angle on a “side-face” of a cube. And what I’ve done is that I’ve placed three empty GameObjects that are always placed on:
The middle of a face of the cube, the edge of the cube next to the first one and lastly an angle anchor which has the same position as the first one except +2 on the z axis.

What I want to achieve is two things:

  • Be able to calculate the angle of the face where these empty GameObjects are using Vector.Distance and Law of Cosines.
  • And the more difficult one is to be able to set the angle of said face in any given angle…

I know what to do with the first one if this was made using a regular calculator, but I’m not sure how to achieve the same effect in Mono C#. Especially Mathf.Cos because it has to be cos^-1…

Here’s what I’ve managed to cook up right now, and without moving any of the edges the value should be 90 degrees but for some reason it returns 50 point something.

distA = Vector3.Distance(FrontAnchor.transform.position, FrontAngle.transform.position);
        distB = Vector3.Distance(FrontAnchor.transform.position, leftFront.transform.position);
        distC = Vector3.Distance(FrontAngle.transform.position, leftFront.transform.position);

        angleFront = Mathf.Cos((Mathf.Pow(distA, 2) - Mathf.Pow(distB, 2) - Mathf.Pow(distC, 2)) / 2 * (distC * distB)) * Mathf.Rad2Deg;

Formulas for this can be found on this Wikipedia: Law of cosines - Wikipedia

I’ve been using this formula from a video:


It’s in Swedish but the goal in this formula is to decide the angle of A, and all we know is the distance from each point of the triangle.

So I’m not sure what sort of situation you’re describing. The angle of the face with respect to what?

As for the cos^-1, that is called ‘inverse cosine’, also known as ‘arc cosine’. Unity has this method as Mathf.Acos:

Now mind you, I am particularly lazy… I dont see much reason in overworking my brain at this point. :wink:

distA = Vector3.Distance(FrontAnchor.transform.position, FrontAngle.transform.position);
        distB = Vector3.Distance(FrontAnchor.transform.position, leftFront.transform.position);
        distC = Vector3.Distance(FrontAngle.transform.position, leftFront.transform.position);
angleA = Vector3.Angle(FrontAnchor.transform.position, FrontAngle.transform.position);
angleB = Vector3.Angle(FrontAnchor.transform.position, leftFront.transform.position);
angleC = Vector3.Angle(FrontAngle.transform.position, leftFront.transform.position);

Thanks for the replies, I’m looking into it now trying to figure it out.

Basically I’ve programmed my own cube and I’m using handles on each edge of the cube to adjust the shape. And what I’m trying to do now is being able to set a specific angle to one of these faces by adjusting the edges accordingly. But for starters I’m trying to find out what the current angle is by manually adjusting the handles in the editor.

You keep saying “set an angle to these faces”.

Angle isn’t enough information to do anything with.

If I told you I rolled a die, and the 6 face was at 90 degrees. What does that even mean? 90 degrees what? 90 degrees about the axis orthogonal to the 6-face? 90 degrees off what origin orientation, what is 0 degrees look like?

An angle describes the relationship between 2 orientations.

Anyways, my entire point is that the first key to programming and math is being able to describe the specifics of what you’re trying to calculate. And many people on these forums are always asking “how do I calculate the angle of this”? The angle of what? Off what?

I figured it out, thanks to the answer with Mathf.Acos, but to answer your question @lordofduct and to anyone else finding this thread in the future:

(Sorry for the crude artstyle, wanted to make it quick)


To this cube there’s a child object for each edge, there’s also child objects for each face that is offset from the cube on the x/y/z by one depending on which side it is.


And what I’m doing is calculating the angle of the face (of the cube between the edges) by measuring the distance between all three and using the following formula to find out the angle:

angle = Mathf.Acos(((distAB * distAB) - (distBB * distBB) - (distCB * distCB)) / (2 * (distBB * distCB))) * Mathf.Rad2Deg;

And later on I’m subdiving this value and fiddling with it to make it so that when the face is flat it’s supposed to read 0 degrees, and if you adjust the edges in any way the value will adjust. For example adjusting the left side will yield +something to the angle, and the right side will yield -something to the angle.