Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

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:
    529
    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:
    529
    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.