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 commodore · Jun 24, 2012 at 01:06 AM · angle

How to find how much an object has rotated?

Hey, I'm trying to find the angle of an object from a certain point. I've tried a few things nothing seems to be working properly. I suck at explaining so here's a little drawing of what I mean:

alt text

car_example.jpg (45.3 kB)
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by whydoidoit · Jun 24, 2012 at 01:16 AM

   var findAngle = false;
   var findingAngle = false;
   var lastPosition : Vector3;
   var currentAngle : float;

   function Update() {

          if(!findAngle) findingAngle = false;
          if(findAngle && !findingAngle) {
              lastPosition = transform.forward;
              findingAngle = true;
          }
          if(findingAngle) {
               currentAngle = Mathf.Acos(Vector3.Dot(lastPosition, transform.forward));
          }

 
   }

This tells you the angle between the two vectors - but it's only good for 180 degrees and it will be positive no matter which way the rotation happens - did you want something else?

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 commodore · Jun 24, 2012 at 01:25 AM 0
Share

This might work, let me give it a shot!

avatar image commodore · Jun 24, 2012 at 01:45 AM 0
Share

hm... there's no way to check if it goes past 180 either way is there?

avatar image Mizuho · Jun 24, 2012 at 01:51 AM 0
Share

@commodore: Best way to do it is to use the vector difference between the two points and Pythagorean's Theorem.

avatar image whydoidoit · Jun 24, 2012 at 02:21 AM 0
Share

@commodore Ah damn I was afraid you were going to say that :)

avatar image whydoidoit · Jun 24, 2012 at 02:24 AM 0
Share

Try this:

  var findAngle = false;
   var findingAngle = false;
   var lastPosition : Vector3;
   var currentAngle : float;
 
   function Update() {
 
          if(!findAngle) findingAngle = false;
          if(findAngle && !findingAngle) {
              lastPosition = transform.right;
              findingAngle = true;
              currentAngle = 0;
          }
          if(findingAngle) {
               var newAngle = $$anonymous$$athf.Acos(Vector3.Dot(lastPosition, transform.forward));
               currentAngle += (newAngle-90);
               lastPosition = transform.right;
          }
 
 
   }
Show more comments
avatar image
0

Answer by Mizuho · Jun 25, 2012 at 05:47 AM

 #pragma strict
 
 var findAngle = false;
 var reference : Vector3;
 var angle : float;
 
 function Update () {
     if(Input.GetKeyDown(KeyCode.N)) findAngle = !findAngle;
     if(findAngle) {
         reference = new Vector3(1, 0, 0); // Direction 0 degrees is in (currently positive x axis)
         angle = Vector3.Angle(reference, transform.position);
         if(transform.position.z - reference.y < 0) {
             angle = 360 - angle;
         }
     }
 }

Here's the updated function. It uses the positive x axis as 0 and counts degrees counter-clockwise.

Edit: Cleaned the code up.

Comment
Add comment · Show 9 · 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 commodore · Jun 27, 2012 at 12:59 AM 0
Share

$$anonymous$$aybe I'm just implementing your code wrong. It only gives me a value around 1.4.. Thanks for helping and sorry if I'm annoying or too much of a noob to understand

 if(Input.Get$$anonymous$$ey("h"))
 {
     findAngle = true;
 }
 else
 {
     findAngle = false;
 }    
 
 if(findAngle) 
 {
     // Or whatever point you're basing this off...
     reference = Vector3.zero;
     vector = transform.position - reference;
     angle = $$anonymous$$athf.Atan(vector.z / vector.x);
     if(vector.z > 0) angle += 180;
 }
 
 Debug.Log("Angle since findAngle turned true: "+angle);
avatar image Mizuho · Jun 27, 2012 at 01:06 AM 0
Share

Gimme a second to throw this into my testing grounds then. I'll be back in about 10 $$anonymous$$utes...

avatar image commodore · Jun 27, 2012 at 01:08 AM 0
Share

Thank you! I'll take a little screencap of my problem. It's most likely a problem on my end.

avatar image commodore · Jun 27, 2012 at 01:24 AM 0
Share

I included the debugger so you could see when I'm pressing "h"

http://www.youtube.com/watch?v=0sfc$$anonymous$$IYt6j4&feature=youtu.be

avatar image Mizuho · Jun 27, 2012 at 01:34 AM 0
Share

That took a looong time (sorry, I became very bad at math), but I figured out a foolproof way of doing it! See my updated answer.

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

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Best way to calculate angle between series of Vector3 points 0 Answers

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

Material doesn't have a color property '_Color' 4 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