Search Unity

Vector3.Angle confusion

Discussion in 'Scripting' started by barinelg, Sep 22, 2014.

  1. barinelg

    barinelg

    Joined:
    Jun 1, 2010
    Posts:
    95
    Hey everyone!

    Please see the image below:

    The blue line is the forward vector. The red line is the right vector.

    I want to get the degrees between Green and Red. When using Vector3.Angle with these two vectors, it tells me the angle is 115 degrees. How? What am I missing?
     
  2. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Please show the code, or at least pass the actual numbers from both vectors.
     
  3. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    The angle could be 115 degrees. Since we're looking at a two dimensional picture I'm not convinced the green line is on the same plane as the red and blue lines.

    So yeah, what @Dantus said. =)
     
  4. barinelg

    barinelg

    Joined:
    Jun 1, 2010
    Posts:
    95
    This is how it's being called. I don't have values on hand right now. I'll try to provide more shortly, but I did try to restrict to 2 axes. The idea is I'm trying to decide where green is hitting the character.
    Code (csharp):
    1. Vector3.Angle(new Vector3(green.x, 0.0f, green.z), new Vector3(red.x, 0.0f, red.z)
     
  5. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    I mean actual numbers for all three components of the vector.
     
  6. barinelg

    barinelg

    Joined:
    Jun 1, 2010
    Posts:
    95
    At the time I did not have values, so I provided a basic overview of the planes used. The values provided here are not the ones for the image.
    Code (csharp):
    1. Vector3.Angle(new Vector3(-0.8f, 0.0f, -0.6f), new Vector3(0.8f, 0.0f, -0.2f));
     
  7. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    If those numbers give you 110 degrees, then that seems like it's about the right answer. The first vector is diagonal down-left, the second is right and slightly downward. Guesstimating the angle between those puts it pretty close to 110 degrees.

    .....but that's not even remotely close to the input values of what that image is showing. In that image, the green would be roughly (0.4,0,0.8) and the red would be (1,0,0). If those input values give you 110 degrees, then something is definitely going wrong.
     
  8. barinelg

    barinelg

    Joined:
    Jun 1, 2010
    Posts:
    95
    Unfortunately I can't provide the exact values of the first image. What I can do is make a few images and provide the info for each one. That last example with the included values was 129deg. Follow-up with extra images coming shortly.
     
  9. barinelg

    barinelg

    Joined:
    Jun 1, 2010
    Posts:
    95

    Code (csharp):
    1. Vector3.Angle(newVector3(-0.6f, 0.0f, -0.8f), newVector3(0.8f, 0.0f, -0.4f));
    Calcuated: 103.8714


    Code (csharp):
    1. Vector3.Angle(newVector3(0.3f, 0.0f, -1.0f), newVector3(0.9f, 0.0f, -0.1f));
    Calcuated: 68.88651
     
  10. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Those numbers don't seem to have any relation to where the red and green lines are in your images.

    I mean, even if you don't understand vectors, think about it.... both vectors in your examples are completely different from the other example, but the red line has not moved. How could the numbers possibly be right?
     
  11. barinelg

    barinelg

    Joined:
    Jun 1, 2010
    Posts:
    95
    I did one more, ensuring that the lines are created and kept the exact moment the calculations are made.


    Code (csharp):
    1. Vector3.Angle(new Vector3(-0.7f, 0.0f, -0.7f), new Vector3(0.8f, 0.0f, 0.0f));
    Green is the left value, red is the right. Calculated: 132.3842

    The point is the position of the [GameObject] at the time of the vector calculations and angle calculation. This was the exact moment of the calculations. The value should be about 42, but it's not. If green was mirrored, the value would be about 42. Basically it seems that the values are always 90 degrees off. I could always flip something or offset it, but just trying to clear up the reason behind the values with what I'm seeing.
     
  12. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    That seems like it's what's happening. In order to provide any more insight I would have to see the code that is involved in A) creating these vectors, and B) displaying them as in the screenshots. They are obviously incongruous with each other, and that might be the source of your confusion.
     
  13. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    Code (csharp):
    1. Vector3.Angle(new Vector3(-0.7f, 0.0f, -0.7f), new Vector3(0.8f, 0.0f, 0.0f));
    If I'm looking at the xz plane, (-0.7, 0, -0.7) points to the lower left while (0.8, 0, 0) points right.

    This means the green line in the picture should be going the opposite way, down-left, from the origin than your pictures indicate. Possibly the direction vector of your green line is negative when it shouldn't be. Anyways, your Vector3.Angle() input and your green line aren't matching.
     
    Last edited: Sep 22, 2014
  14. barinelg

    barinelg

    Joined:
    Jun 1, 2010
    Posts:
    95
    I see what you're saying Garth, and it looks like you're correct. I wasn't using the same origin point, which completely escaped me. What happens when you stare at something too long. x.x Thanks!
     
    GarthSmith likes this.