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 razialooo · Jun 19, 2015 at 03:56 AM · degreescalculationmeasure

Degree measurement between two objects : bug

Hello everybody,

i am in big need to measure the relation between object A and Object B in terms of degrees. What do i mean? Imagine a compass. You stand somewhere (your Character Object), you rotate and it shows towards north --> you can now how your relation is towards north. In my implementation north would be exactly 180 degree: than i would face it. If i stand to the opposite it would be 0.

Now i don't want the degree relation between the Character and North, but between him and different Collides in the Game. So i can direct him towards them.

I have kind of working code, but it has few bugs, i will describe below it

     float sign;
     float angle;
     Vector3 targetDir;
     Vector3 forwardDir;
     Vector3 referenceRight;
     float finalAngle;
     Transform target;
 
     void Start ()
     {
         target = GameObject.Find("Cube").transform;
     }
 
     void Update () {
         targetDir = target.position - transform.position;
         forwardDir = transform.forward;
         angle = Vector3.Angle(targetDir, forwardDir);
 
         referenceRight = Vector3.Cross(Vector3.down, forwardDir);
         sign = Mathf.Sign(Vector3.Dot(targetDir, referenceRight));
 
         finalAngle = sign * angle;
 
         if (finalAngle < 180) { finalAngle = finalAngle + 180; }
         else if (finalAngle > 180) { finalAngle = finalAngle - 180; }
 
         Debug.Log(finalAngle);
 
     }

This code is attached to the first person Player and i have tested it with Small and Big Colliders, Cubes and so on. And it works just fine, except for one bug.

BUG:

  • If i am standing close to the collider or the Cube or what ever is my target for degree calculation, the degree will jump between let's say 160-180.

  • If i go even closer, it will jump between 150-190!

The closer i am to the target, the more degrees are jumped over in the front !!!

I would really appreciate any solution. I thought about making a very small object in the collider towards it should point, but it did not help. I decreased the body of the first person character and made him very thin, did not help either ...

thank you very much in advance :)

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
1
Best Answer

Answer by derPuppeteer · Jun 21, 2015 at 07:10 PM

Hi, I think your problem is that you are comparing the angle between 2 3d Vectors. This should do the trick:

          targetDir = target.position - transform.position;
          forwardDir = transform.forward;
          //new stuff
          targetDir.y = fowardDir.y;
          //end of new stuff
          angle = Vector3.Angle(targetDir, forwardDir);
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 razialooo · Jun 21, 2015 at 08:29 PM 0
Share

works ! thx :)

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

[2D] Get 0-359 compass angle player <--> object 1 Answer

How to determine the number of pixels per unit? 1 Answer

Detect when audio thread is taking too long 1 Answer

Lerp - Time Remaining 1 Answer

MouseOrbitImproved camera distance seemingly inverted 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