Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by 1337GameDev · Dec 24, 2011 at 09:15 PM · rotation3danglevector2worldspace

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.

  1. 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?

  2. 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?

  3. How cam I accommodate for this odd effect and what does it mean?

  4. 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?

  5. 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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
4

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.

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Dec 25, 2011 at 12:43 AM 0
Share

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.

avatar image 1337GameDev · Dec 25, 2011 at 07:48 AM 0
Share

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 :(

avatar image aldonaletto · Dec 25, 2011 at 06:50 PM 1
Share

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:
alt text
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.

avatar image 1337GameDev · Dec 25, 2011 at 09:07 PM 0
Share

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?

avatar image aldonaletto · Dec 26, 2011 at 09:14 PM 1
Share

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!)

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

3D Turret Rotation 0 Answers

Rotate an object around another object at an angle from the X axis? 1 Answer

Derive world space rotation given 2 points in space? 0 Answers

Rotate an object from 2 Vector2s? 1 Answer

Moving forward in world space with rotation. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges