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 Chimera3D · Jul 21, 2012 at 12:04 AM · rotationpositionvector3anglesdegrees

Quick Angles Question

I have a code where I'm getting an angle between two Vector3's, however the angles returned come in the form of numbers like .011340 or .19872009752 as opposed to 90 degrees or 180 degrees. Can someone tell me what I'm doing wrong? The two variables used here are Vector3's:

generalDirection = Vector3.Angle (transform.position, desiredPosition);

EDIT: Sorry but now the angle starts at 35, goes up to 135 on the other side and goes back down to 35 with a full clockwise rotation. I'm confused.

EDIT: Ok I fixed that, now I just need help making one side negative and the other positive

Comment
Add comment · Show 4
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 Seth-Bergman · Jul 21, 2012 at 12:50 AM 0
Share

aldo's code should work if this script is attached to the object at the vertex of the angle, otherwise replace vectors with the appropriate version of:

position1 - vertexPosition , position2 - vertexPosition

avatar image Seth-Bergman · Jul 21, 2012 at 01:42 AM 0
Share

something like:

if(generalDirection > 180) generalDirection = -(360-generalDirection);

avatar image Chimera3D · Jul 21, 2012 at 07:53 AM 0
Share

I've tried that already.

EDIT: Because the value never goes higher than 180 it just get's inverted and starts going back down until hits 0 on the other side.

avatar image Chimera3D · Jul 21, 2012 at 07:58 AM 0
Share

Nvm figured something else out. Thanks for all the help!

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by aldonaletto · Jul 21, 2012 at 12:15 AM

Vector3 is a structure used to store many 3 float values, like points, vectors, angles etc. Transform.position and desiredPosition are points, not vectors, and Vector3.Angle is intended to be used with vectors. In this particular case, you're getting the angle between the vectors (0,0,0)->transform.position and (0,0,0)->desiredPosition, which normally makes no sense. If you want to measure the angle between the transform forward direction and the direction from the transform to the desiredPosition, you should use this:

 generalDirection = Vector3.Angle(transform.forward, desiredPosition-transform.position);

A point is a position in the 3D world, while a vector is a direction - you can think of a vector as an "arrow" based on 0,0,0 and pointing to the desired direction. In order to calculate the vector that points the direction from position A to position B, subtract A from B:

 directionAtoB = pointB-pointA;

The resulting vector shows the direction A to B, and its length (directionAtoB.magnitude) is the distance between A and B.

Comment
Add comment · Show 1 · 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 Chimera3D · Jul 21, 2012 at 12:24 AM 0
Share

Thanks for that information!

avatar image
1

Answer by Seth-Bergman · Jul 21, 2012 at 12:17 AM

see the script reference here:

http://docs.unity3d.com/Documentation/ScriptReference/Vector3.Angle.html

they use two directions, not two points.. if you think about it, the angle based on two points in space would be relative to the point of origin..

Comment
Add comment · 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

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

Trying to get the angle of a camera object based on players forward, is not distance independent 0 Answers

Relative Rotation 1 Answer

How to calculate an end point of a line after rotating an angle in three-dimensional space? Thanks! 1 Answer

Vector3.Lerp moves other characters over network to (0, 0, 0) 1 Answer

How do I reset a GameObject pos / rot with a toggle? 0 Answers


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