Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

How to get the angle betwwen 2 directions

Discussion in 'Scripting' started by MikeyJY, Jun 17, 2020.

  1. MikeyJY

    MikeyJY

    Joined:
    Mar 2, 2018
    Posts:
    530
    I know that I posted a similar thread to this but I'm 100% sure that I'll will receive no answers anymore, so I decided to make another post but this time is different.


    Code (CSharp):
    1. Vector3 direction = transform.position - FPSCharacter.transform.position;
    2.  
    3. angle = Vector3.Angle(direction, FPSCharacter.transform.forward );
    That code is from documentation page.
    Code (CSharp):
    1. private void OnDrawGizmos()
    2.     {
    3.         if (gameObject.tag == "Hatchet")
    4.         {
    5.             Gizmos.color = Color.blue;
    6.             Gizmos.DrawRay(FPSCharacter.transform.position, FPSCharacter.transform.forward * 100);
    7.             Gizmos.color = Color.white;
    8.             Gizmos.DrawRay(FPSCharacter.transform.position, transform.position - FPSCharacter.transform.position);
    9.         }
    10.     }
    I also draw some gizmos to test if the directions are correct and they are:


    Can you explain me why it says that the angle 60 deg, because it is very clear that the angle between the blue line and white line is under 20 deg?

    Maybe I didn't understand what Vector3.Angle exactly returns. And if I didn't what is the method to return the 2D angle of 3D Vectors?
     
  2. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    From what I can see, it says the angle is 37 degrees which looks about correct. What is it you are trying to achieve here? I'm guessing you are looking for a way to tell if the object can be picked up or highlighted. If so, I would suggest a raycast from the camera instead as distance and angle could just end up making it valid for multiple objects to be collected/highlighted at the same time.
     
  3. MikeyJY

    MikeyJY

    Joined:
    Mar 2, 2018
    Posts:
    530
    I can't use raycast bacause some object doesn't have collider.
    Look at the end of recording. I'm looking directly to the object and the inspector says that the angle is 60.