title: Find angle between two objects while taking another object as center in Unity using C#
body: <p>I am trying to find angle between two stationary objects while taking a moving object as center. I have tried a lot like finding distance between all the objects and then applying <a href="http://www.teacherschoice.com.au/maths_library/trigonometry/solve_trig_sss.htm" rel="nofollow noreferrer">cosine rule</a> to find angle but it calculates the wrong angle. This is the code. First it calculates the desired angles position(greatest, smallest or medium), then calling calculate angle.</p>
  
  <p><a href="https://i.stack.imgur.com/CcDYY.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/CcDYY.png" alt="enter image description here"></a></p>
  
  <pre><code>void GenerateAngle()
  {
  if (transform.position != currentPos)
      {
          float angle = 0;
          distancePillarRToPillarL = Vector3.Distance(PillarR.position, PillarL.position);
          distancePlayerToPillarL = Vector3.Distance(PillarL.position, transform.position);
          distancePlayerToPillarR = Vector3.Distance(PillarR.position, transform.position);
          print(distancePillarRToPillarL + "  " + distancePlayerToPillarL + "  " + distancePlayerToPillarR);
  
          if (distancePlayerToPillarR &gt; distancePlayerToPillarL &amp;&amp; distancePlayerToPillarR &gt; distancePillarRToPillarL)
          {
              if (distancePillarRToPillarL &gt; distancePlayerToPillarL)
              {
                  angle = CalculateAngle(distancePlayerToPillarR, distancePillarRToPillarL, distancePlayerToPillarL, AnglePos.Medium);
              }
              else if (distancePlayerToPillarL &gt; distancePillarRToPillarL)
              {
                  angle = CalculateAngle(distancePlayerToPillarR, distancePlayerToPillarL, distancePillarRToPillarL, AnglePos.Smallest);
              }
          }
          else if (distancePlayerToPillarL &gt; distancePlayerToPillarR &amp;&amp; distancePlayerToPillarL &gt; distancePillarRToPillarL)
          {
              if (distancePillarRToPillarL &gt; distancePlayerToPillarR)
              {
                  angle = CalculateAngle(distancePlayerToPillarL, distancePillarRToPillarL, distancePlayerToPillarR, AnglePos.Medium);
              }
              else if (distancePlayerToPillarR &gt; distancePillarRToPillarL)
              {
                  angle = CalculateAngle(distancePlayerToPillarL, distancePillarRToPillarL, distancePlayerToPillarR, AnglePos.Smallest);
              }
          }
          else if (distancePillarRToPillarL &gt; distancePlayerToPillarL &amp;&amp; distancePillarRToPillarL &gt; distancePlayerToPillarR)
          {
              if (distancePlayerToPillarL &gt; distancePlayerToPillarR)
              {
                  angle = CalculateAngle(distancePillarRToPillarL, distancePlayerToPillarL, distancePlayerToPillarR, AnglePos.Largest);
              }
              else if (distancePlayerToPillarR &gt; distancePlayerToPillarL)
              {
                  angle = CalculateAngle(distancePillarRToPillarL, distancePlayerToPillarR, distancePlayerToPillarL, AnglePos.Largest);
              }
          }
          print(angle);
          currentPos = transform.position;
      }
  
  
  }
  
  
  
  float CalculateAngle(float largest, float medium, float smallest, AnglePos pos)
  
  
   {
          float largestAngle = (Mathf.Acos((Mathf.Pow(medium, 2) + Mathf.Pow(smallest, 2) - Mathf.Pow(largest, 2)) / (2 * medium * smallest))) * Mathf.Rad2Deg;
          float angle;
          switch (pos)
          {
              case AnglePos.Medium:
                  {
                      angle = Mathf.Asin((medium * Mathf.Sin(largestAngle)) / largest) * Mathf.Rad2Deg ;
                      return angle;
                  }
  
          case AnglePos.Smallest:
              {
                  angle = Mathf.Asin((smallest *  Mathf.Sin(largestAngle)) / largest) * Mathf.Rad2Deg;
                  return angle;
              }
  
          default:
              {
                  return largestAngle;
              }
  
      }
  </code></pre>
  
  <p>}</p>