- Home /
Vector2.Angle questions and confusion.
I am curious exactly how this method works, Vector2.Angle. I know it takes in 2 parameters, bother vector2 objects, but what does it do to calculate the angle? I used this function in my camera rotation script, for android, and used the positions of two touches to rotate camera if it got bigger or smaller. I also had a debug string output to a guiText object in my scene and got odd results.
When my fingers are at a 45 degree angle (diagonal and not horizontal / vertical) the angle was said to be zero. Why could this be? Could it be due to my main camera not being horizontal or vertical?
When my fingers are close to each other, the angle decreases. I keep them horizontal, but if i pinch in, the angle decreases, and if i pinch out, the angle increases, why is this?
How cam I accommodate for this odd effect and what does it mean?
When i take the angle of two points, does it form a triangle between the two points, or does it tell the orientation (according to being vertical or horizontal) of these two points?
when thinking in 3d space, what does vector2.angle tell you, if i split up rotations into up/down (one plane) and left/right (2nd plane)? I am curious because i wnat to make an object chase another and want to use angles to see if it is facing the object based on its, and target object's position. (not using vector 3 as i don't know how to interpret what that angle means in 3d space as i would assume it needs two components that display 2 angles)
Sorry if these questions are vague or i am asking too much, i figured it would fit under this topic as it helps in the understanding of how Vector2.Angle (and an idea of Vector3) works.
Answer by aldonaletto · Dec 24, 2011 at 11:58 PM
Vector2.Angle(v1, v2) assumes that v1 and v2 are vectors, not positions - think of them as arrows based on (0,0) and pointing to v1 and v2. In screen coordinates, if v1 is at the top-left and v2 is at bottom-right, Vector2.Angle(v1,v2) will return 90 degrees; if your fingers are in the same line from (0,0), it will return 0 degrees, because the 2 vectors will point the same direction (the length, which is the distance from screen origin, doesn't interfere with the angle).
It would be better in this case to use Vector2.Distance(v1,v2), because Distance assumes that v1 and v2 are positions - it will accurately report a increasing value when the fingers are getting away from each other, and a decreasing value when they're getting closer.
Correct ;) If you want to use Vector2.Angle, you have to offset the two positions so they are relative to the screen center (or any other reference point). Just subtract the half of the screen size and it should work. Now your vectors start at the screen center and end on your two touch-points.
Hmm still a bit confused, any visul representations? Is the exampe, v1 is on top left and v2 in bot right, how is that 90 degrees? It is a diagonal line? Vectors pointing to 2 opposite corners that r inverse of each other. How does both vects being same direct, lets say going right horizontally, how does increasing the length increase the angle? Can somebody make a visual representation and examples, like simply in paint, to demonstrate dif scenarios? Im still a bit confused. Ive had physics and know about vectors, but we only made vectors into triangles to add, su tract, scale, or figure out resultants. I really wish the documentation for unity functions and etc would be more thorough :(
I don't know exactly where the screen coordinates origin is in a mobile device. Let's suppose the screen is in landscape mode, and has width 480 and height 360. If the bottom-left coordinates are 0,0, then top-left will be 0,359 and the bottom-right 479,0. Passing these two points to Vector2.Angle, the function will interpret them as vectors: one from 0,0 to 0,359 and the other from 0,0 to 479,0. Since the first vector is vertical and the second horizontal, the angle returned will be 90 degrees.
Let's see a more generic case where V1 and V2 are the coordinates of two touches:
As you can see, the Angle function considers V1 and V2 as vectors based on 0,0, and returns the angle between them, while the Distance function considers V1 and V2 as positions in the screen, and returns the euclidean distance between them.
As you suspected, the screen orientation influences the result: according to the docs, if the orientation changes, the touch coordinates also change in order to follow the new coordinate system, which will produce a different angle - this doesn't happen with the Distance function, which will report the same distance no matter which's the screen orientation.
Thank you. Im glad you spent the time to explain that. Huge help with pictures. $$anonymous$$any many thanks! You sir are a hero :p
Does vector 3 angle mthod return two float values (as a vector2?)? How does vector3 work in this sense?
If you want to define a direction with two fingers, it's better to generate a vector from the touch coordinates - something like this:
Vector2 dir = touch1 - touch2;
This will create a vector from touch2 to touch1, and you can use it to define the direction - but this depends highly on what exactly you intend to do. You should elaborate better your idea and post a new question (this one is getting too crowded!)