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
0
Question by Conny14156 · Feb 27, 2013 at 10:21 AM · c#vectormathf

Math help if possible, angle calculation

Hi, Am wondering if anyone could help me with some math if possible. Am trying to calculate the angle differency between my "vision" (Camera Y) and direction between objectPosition - myPosition, I got help with this math so am not 100% sure if I know it outside in(my school math havent cover this section yet/or if it even will), from a little googling I found this http://www.euclideanspace.com/maths/algebra/vectors/angleBetween/index.htm with angle = acos(v1•v2) and I also found http://answers.unity3d.com/questions/119313/Calculating-the-Angle-between-two-vectors.html Even thou I remade my formular exactly the same it still won't work. my camera won't get any different value except 1 after the normalizing,although I get the 0>360 value from the rotation. I am currently using the first person unity standard script that is NOT modified by any means.

But I know I get the correct input from the rotation and it does indeed change. I know that normalizing is suppose to "minimize" vector into a |1| direction vector.(I think) Help and some explanation would be awesome >.<

I tried to do this after I googled. My viewDirection always stays 1 after normalizing,

     viewDirection = transform.eulerAngles;
     objectDirection = transform.position;
     
     viewDirection.Normalize();    
     objectDirection.Normalize();
          
     
     d = Mathf.Acos(Vector3.Dot(viewDirection,objectDirection));
     d *= Mathf.Rad2Deg;
Comment
Add comment · Show 1
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 Fattie · Feb 27, 2013 at 11:36 AM 0
Share

you haven't learned everything about trig in high school math ?

that' amazing

I urge you to (A) leave a note at your school "You are all idiots, goodbye" (B) do nothing but study math, hard, for three months. you will then know everything about math.

unless you are incredibly good at math, you have very little power in the world. math is power. Good luck!

1 Reply

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

Answer by fafase · Feb 27, 2013 at 11:19 AM

I would do it like this:

 public Transform target;
 float ang;
 void Update () {
     float angInCos = Vector3.Dot(transform.forward,(target.position - transform.position).normalized);
     ang = Mathf.Acos(angInCos);
     ang = ang *Mathf.Rad2Deg;
     Vector3 cross = Vector3.Cross(transform.forward,(target.position - transform.position).normalized);
     if(cross.y<0)ang *=-1; 
     print (ang);
 }

Dot returns the cosin of the angle which is between -1 and 1. 0 being 90 degrees. The arcos of the dot returns the angle in radians, multiplying by the rad2deg you get it in degrees.

But, you would notice that you never get negative values, which could be fine for you, but then if you need to know whether it is right or left you use the Cross product which returns a vector3. Checking the value of the y component, you know if the agle is positive or negative.

I chose here, if cross.y is negative then your angle is negative and the target is on the left of the player.

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

11 People are following this question.

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

Related Questions

Lerp Color between 4 corners 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Flip over an object (smooth transition) 3 Answers

C# How Add new score to old score in update function 2 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