title: Calculate target angle given current angle and target position?
body: <p>I've been struggling with what I think should be a very simple problem:</p>
  
  <p><img src="https://i.imgur.com/1MM8aAa.png" alt="Diagram of the problem">.</p>
  
  <p>I know the current heading angle (say 15 deg), and given a target gameObject's transform, I want to calculate the angle that I should rotate towards to face the target. In the above example I want to calculate say 335ish deg.</p>
  
  <p>Note that I need to calculate the target <em>angle</em>, I have another class that, given an angle, takes care of rotating the heading to the desired angle. I'm aware of the Transform.LookAt() and other similar functions, they don't apply in this situation because reasons (in the other class I basically calculate a Euler Quaternion and Lerp until I reach the target angle, due to project constraints, Transform.LookAt() doesn't work).</p>
  
  <p>In the first approach, I tried calculating angle theta with the Dot product of Vector3.forward and the direction vector, didn't quite work right (either rotated forever or in the wrong direction)</p>
  
  <p><img src="https://i.imgur.com/X8W7xDQ.png" alt="Diagram of the problem"></p>
  
  <pre><code>targetAngle = Vector3.Angle(Vector3.forward, (target pos - current pos).normalized);
  </code></pre>
  
  <p>I thought maybe <strong>if</strong> I could calculate the current heading direction vector, I could take the Dot product of that and the target direction vector to get angle theta, then use theta and the current angle to figure out the target angle (360 - theta or something?) to get the angle I want to rotate to. Thing is, I only have the current angle and current pos, I don't understand how to calculate the current heading direction vector from that info. Do I just add some arbitrary constant to the current position's Z value and subtract from that the current position, to get a dir vector? Seems hacky and like it shouldn't work.</p>
  
  <p><img src="https://i.imgur.com/J5FlrLi.png" alt="Diagram of the problem"></p>
  
  <p>Any ideas are welcome.</p>
  
  <p><strong>Edit: Additional Info</strong></p>
  
  <p>The orientation code /u/Asad asked about:</p>
  
  <pre><code>    // Calculate the target Quat to rotate all children by
      Quaternion targ = Quaternion.Euler(0, 0, targetAngleHeading);
      // Calculate the Linear interpolation to apply to all children
      Quaternion lerp = Quaternion.Lerp(children[0].transform.localRotation, targ, Time.deltaTime * speedHeading_dps);
  
      foreach (GameObject c in children)
      {
          // Apply lerp calcualted above to all children
          c.transform.localRotation = lerp;
      }
      // Update the current heading angle 1st child's local z angle
      currentAngleHeading = turrets[0].transform.localEulerAngles.z;
  </code></pre>
  
  <p>Then in FixedUpdate() I have:</p>
  
  <pre><code>    if (currentAngleHeading != targetAngleHeading)
      {
          DesiredHeading();
      }
  </code></pre>