I have fixed object, around its an object is moving in circular shape. Now i am willing to get my moving object angle with respect to fixed boject like form 0 to 360. How can i do this.

enter image description here

i tried this code snippet

Vector3 targetDirection = camHead.transform.position - transform.position;
            angle = Vector3.Angle(targetDirection, transform.forward);

But it is not giving what i need. Sorry i am null in vector math and trigonometry, a trick will be prefer with explanation.

  • \$\begingroup\$ Use transform.up instead \$\endgroup\$ – Bálint May 23 '17 at 7:52
  • \$\begingroup\$ what, where, how? \$\endgroup\$ – Muhammad Faizan Khan May 23 '17 at 7:58
  • \$\begingroup\$ Instead of transform.foward in your code \$\endgroup\$ – Bálint May 23 '17 at 9:27
  • 2
    \$\begingroup\$ @mohammad-faizan-khan : It's very disappointing to make efforts to help other and not getting any feedback. If you don't do it for the ones who answer you, do it at least for future readers. \$\endgroup\$ – Hellium May 27 '17 at 8:36
  • 1
    \$\begingroup\$ @Hellium, you make a good point, but rest assured that in such situations, a clear lack of quality is noticed by the system. Enough poor quality questions, and a user simply won't be allowed to ask more questions. I will be back to up-vote your answer, when I have my votes replenished. \$\endgroup\$ – Gnemlock May 29 '17 at 6:01

@kolenda is right, but his answer is incomplete.

Here is an utility function to have a signed angle (using atan inside this function would have been possible). You will have to provide a normal vector in order to be able to know the sign of the angle :

public static float SignedAngle( Vector3 from, Vector3 to, Vector3 normal )
    // angle in [0,180]
    float angle = Vector3.Angle( from, to );
    float sign = Mathf.Sign( Vector3.Dot( normal, Vector3.Cross( from, to ) ) );
    return angle * sign;

Be "careful", this function will return the angle between -180 and 180, not 0 and 360 (just add 180 to the result)

| improve this answer | |

Use Mathf.Atan2

In general atan2 is an extension of typical arctan function which gives you the correct result in all 4 quarters of a circle, so you don't need to use if's with arctan to compute the angle. You just pass two params - X and Y of your direction vector and you get the angle in Radians.

| improve this answer | |
  • \$\begingroup\$ did you understand the question completely. i suspect that it will not work \$\endgroup\$ – Muhammad Faizan Khan May 23 '17 at 9:20
  • \$\begingroup\$ You've provided a 2D image with 2D description so yes, I think I understood your question. I just assumed you'll be able to use this simple function for your needs, instead of waiting for the code to just copy and paste. Countless people used this function, including myself but you "suspect" it won't work... Have you looked at the link at least? \$\endgroup\$ – kolenda May 23 '17 at 11:40
  • \$\begingroup\$ @MohammadFaizanKhan, why do you suspect it does not work? Your lack of explanation, and the fact that you suspect but did not attempt to disprove leads me to think you may not simply understand this answer, to a degree; which is perfectly fine, provided you provide constructive feedback. Perhaps this answer could be improved, to make it easier to understand? \$\endgroup\$ – Gnemlock May 29 '17 at 6:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.