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 magarcan · Feb 21, 2013 at 02:47 PM · rotationangle

Calc angle.

I need to be calculated the angle between normal to the ground and the one has my object. Take a look: alt text

I've made that script:

 using UnityEngine;
 using System.Collections;
 
 public class PlaneBehaviour : MonoBehaviour
 {
     void Update()
     {
         //Get values
         float horizontal = Input.GetAxis("Horizontal") * Time.deltaTime;
         float vertical = Input.GetAxis("Vertical") * Time.deltaTime;
         
         //Apply
         transform.Translate((Vector3.forward * 100) * Time.deltaTime);
         transform.Rotate(vertical, 0, horizontal);
         
         float angle = -((Vector3.Angle(Vector3.up, transform.forward)) - 90);
         
         Debug.Log(angle);
     }
 }


With my code the angle is being calculated well is I move my object up and down. Problem comes when I rote my object. Instead of have the angle marked in red, I get 90 degrees (marked in blue): 141-anglec.png

Cheers!!

some.png (26.3 kB)
anglea.png (21.6 kB)
Comment
Add comment · Show 8
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 robertbu · Feb 21, 2013 at 04:01 PM 0
Share

I'm not sure what you are asking, but note that Vector3.Angle is not signed, so your calculation will be incorrect for some rotations. If you describe what you are trying to do with the angle, someone on the list can likely give you a solution.

avatar image moghes · Feb 21, 2013 at 04:39 PM 0
Share

So please we need to know some more details, as you told "The angle is being calculated well except when I rotate my object has been rotated horizontally:" So as you run the game you are getting the accurate angle? but why to do

 float angle = -((Vector3.Angle(Vector3.up, transform.forward)) - 90);

and not just simply

 float angle = Vector3.Angle(Vector3.up, transform.forward);

And when you rotate horizontally, you mean around y axis right?

avatar image magarcan · Feb 21, 2013 at 04:54 PM 0
Share

I've upgraded the question and the pictures. I hope now is clearer.

avatar image robertbu · Feb 21, 2013 at 06:25 PM 0
Share

Clearer, but still not enough for me to write an answer. Vector3.Angle will return an unsigned angle representing the shortest distance between the two vectors, so it does not always measure what you want it to measure. Rather than focus on how to get your angle right, you might want to describe the problem you are trying to solve by calculating the angle. There are many different ways of dealing with rotations in Unity.

avatar image magarcan · Feb 21, 2013 at 08:19 PM 0
Share

I've an object that is always moving forward, it can rotate and move up and down over the new position (think in something like a spaceship).

What I want to know is how much has changed the trajectory (up and down from my object) in a period of time.

That could be a good example: alt text

do.png (73.1 kB)
Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by hoy_smallfry · Feb 22, 2013 at 06:15 PM

This looks like a job for... DA DA DA!

The vector dot product!!

alt text

The the result of the dot product between two vectors A and B (A · B) is equal to the cosine of the angle between them (cos(θ)) multiplied by the magnitude of vector A (|A|), or:

|A|cos(θ) = A · B

Then all you have to do is solve for θ, the angle between the A and B:

|A|cos(θ) = A · B

cos(θ) = (A · B) / |A|

θ = acos((A · B) / |A|)

Unity has a dot product function, a vector magnitude function, and a arc cosine (inverse cosine) function, so that you can do this in code. The result is in radians, so if you want degrees Unity also provides a function that converts radians to degrees.

Edit:

I realized that Vector.Angle most likely does this already, so I looked over your code some more, and I realized that you are probably getting 90 degrees in the second image because you are using Vector3.up in this line of code:

    float angle = -((Vector3.Angle(Vector3.up, transform.forward)) - 90);

According to the documentation, Vector3.up is a constant that represents (0, 1, 0). So your second picture is wrong. The actual vectors being used for the calculations are these:

alt text

and that angle between them is 90 degrees. The math is correct; it's your expectation that isn't. If you wanted it to detect the shortest angle among all axes, that's another thing.


8142-some.png (29.5 kB)
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 magarcan · Feb 22, 2013 at 07:10 PM 0
Share

Of course, maths are right. I'm using Vector3.up when I would need it rotated the same than the object. I know that Vector3.up is a constant. In that case I would need a unitary vector rotated 90º

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

12 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 avatar image

Related Questions

Having Trouble with Vector3.Angle 1 Answer

Problem with Z rotation when rotating X and Y and the same time. 2 Answers

Getting the rotation of an object does not return correct rotation 1 Answer

Applying force with respect to angle with the ground? 1 Answer

Look at an angle 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