Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This question was closed Aug 19, 2019 at 04:52 PM by artisKazeeks for the following reason:

Too subjective and argumentative

avatar image
0
Question by artisKazeeks · Aug 18, 2019 at 01:26 PM · vector3angleplane

Get angle between a vector and horizon (x,z plane)

So, I have a Vector3 vector v and I need to get the angle phi (in degrees) between it and the horizon (x,z plane). Obviously the correct way to do it would be to get v projection in x,z plane (Vector2(v.x , v.z)) and then calculate the angle between those 2 vectors.

However, the way I do it right now is:

 float phi = Mathf.Asin(v.y / v.magnitude) * Mathf.Rad2Deg;

Height of a right-angle triangle divided by its hypotenuse equals sin of angle at its base. So, the angle at its base equals arcsin of height divided by hypotenuse.
Of course it gives an error when v.magnitude = 0.

So, I am asking, is there any better method of doing this?

Comment
Add comment · Show 7
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 sacredgeometry · Aug 18, 2019 at 02:12 PM 0
Share

So you are looking to get the angle between a vector and a surfaces tangent.

So first you need to define your plane?

How are you defining it? Is it a mesh? Is it a collection of Vector3s ($$anonymous$$imum of 3)?

avatar image artisKazeeks sacredgeometry · Aug 18, 2019 at 06:54 PM 0
Share

I don't know how else would you call it than horizon. It is the world's x,z plane, so there is no use in defining it as all Vector3s (in world space) are alligned (grid wise) with it. If it helps you understand than those 3 points (for defining a plane) in world space would be (1,0,0), (1,0,1), (0,0,1) (notice that y=const).

avatar image sacredgeometry artisKazeeks · Aug 18, 2019 at 06:58 PM 0
Share

:) So you are going on the worlds horizontal plane you aren't needing to compensate for a variable horizon ok thats pretty straight forward then.

Im just cooking dinner but if it hasnet been answered by the time I get back I will.

avatar image Captain_Pineapple · Aug 18, 2019 at 04:11 PM 1
Share

Not sure but: your post is a bit contradicting.... you want the angle between a vector and a plane. But this angle will never be more or less then 90° since a plane only has a normal but no orientation. If you need 360° of angles then you need the angle between v and a vector, not a plane.


try to clear this up.

avatar image artisKazeeks Captain_Pineapple · Aug 18, 2019 at 06:49 PM 0
Share

I guess it really doesn't matter if it has a range from -90 to 90 rather than -180 to 180. At least not for what I need it for (or so I think). The method of using 2 vectors would make more sense (as I have already mentioned in the question), but Vector3.Angle only returns positive values (but I also need negative ones).

avatar image Dragate · Aug 19, 2019 at 05:02 PM 0
Share

Ins$$anonymous$$d of v.y / v.magnitude, you could use v.normalized.y, which avoids the hassle of try-catching yourself the error of a division by 0.

avatar image furkan59 · Feb 28, 2021 at 08:41 AM 0
Share

You can look at this link

https://www.superprof.co.uk/resources/academic/maths/analytical-geometry/distance/angle-between-line-and-plane.html

1 Reply

  • Sort: 
avatar image
3

Answer by sacredgeometry · Aug 18, 2019 at 07:47 PM

This will get you the angle off level with the horizontal. with perfectly upright being 0 and upside down being 180. Does this meet your requirements?

     void Update()
     {
         Debug.Log(GetHorizonAngle());
     }
 
     float GetHorizonAngle()
     {
         return Math.Abs(Vector3.SignedAngle(Vector3.up, transform.up, transform.forward));
     }
 

Note:

If you want it to start at 90 at the poles are reduce to 0 as you rotate closer to the horizon you can do this instead:

Math.Abs(Vector3.SignedAngle(Vector3.up, transform.up, transform.forward) - 90.0f);

Comment
Add comment · Show 13 · 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 artisKazeeks · Aug 19, 2019 at 01:49 PM 0
Share

Unfortunately can't accept your answer since you use transform ins$$anonymous$$d of a given Vector3 (in my case I am doing physics calculations, so there is no transform, but only one Vector3).
And if I try to make it work with what I have:

 // v - previously precalculated Vector3
 phi = Vector3.SignedAngle(v, new Vector3(v.x, 0, v.z), Quaternion.AxisAngle(90, Vector3.up) * new Vector3(v.x, 0, v.z))

I don't think it is any better way of doing it than what I am already doing.

Another thing is that I don't understand why you use Vector3.SignedAngle ins$$anonymous$$d of Vector3.Angle if you are ignoring negative values anyway.

To make myself clear, you can only use one Vector3, the value (angle) should be 0 when parallel to the horizon, positive when looking upwards from horizon and negative when looking downwards to horizon. Also the method should be more efficient or equivalent to the one I am currently using (arcsin of v.y / v.magnitude).

avatar image sacredgeometry artisKazeeks · Aug 19, 2019 at 01:50 PM 0
Share

transform.up and transform.forward are vectors. i.e they are the forward and up vectors of the thing you are testing against the horizon.

avatar image artisKazeeks sacredgeometry · Aug 19, 2019 at 02:26 PM 0
Share

Imagine that all you have is one Vector3 (forget about any gameobjects!!!). There is no such thing as Vector3.transform, so, where would you get the transform from? YOU DON'T! As I already explained, I am using this for physics, meaning THERE IS NO transform or it is not relevant (it has nothing to do with) to the vector.

Show more comments
Show more comments

Follow this Question

Answers Answers and Comments

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

Rotate floor plane in-game via C# script 1 Answer

How to get the angle of the terrain under de front and back of my object 1 Answer

Angle between player and rocket targetting player. 1 Answer

Global Rotation of a Vector Around an Object 0 Answers

Angle Of Ray 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