Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 nullgobz · Aug 01, 2015 at 03:57 PM · directionangle

Finding the direction from an angle in 3d space

So im trying to find a direction from an angle. Here is what i got so far.

 // Vertical
 Vector3 dir1 = new Vector3(0.0f, Mathf.Sin(Mathf.Deg2Rad * vertAngle), Mathf.Cos(Mathf.Deg2Rad * vertAngle));
 
 // Horizontal
 Vector3 dir2 = new Vector3(-Mathf.Sin(Mathf.Deg2Rad * horzAngle), 0.0f, Mathf.Cos(Mathf.Deg2Rad * horzAngle));

This will give me either a horizontal or vertical direction. However i want to combine these two directions into one. So i have for example 45 deg up and 90 deg horizontally. I tried adding and normalizing, but it dident really work.

Thx for showed interest! :)

Comment
Add comment · Show 3
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 NoseKills · Aug 01, 2015 at 05:56 PM 0
Share

I don't quite understand what kind of result you are looking for because... you have 2 values: horizontal (i.e. how much to the left) and vertical (i.e. how high) and based on this you try to deter$$anonymous$$e a direction in 3D space. I don't believe there's a "natural" way to deter$$anonymous$$e it that way because you'd also need a way or a value to deter$$anonymous$$e how "deep" (along z axis)

avatar image nullgobz · Aug 01, 2015 at 06:07 PM 0
Share

The result im looking for is a direction in 3D space based on two angles. So that i can change the angles independently.

So i want a "horizontal" axis to be always the same then i need to change the "vertical" axis dependent on player position.

avatar image nullgobz · Aug 01, 2015 at 06:12 PM 0
Share

i came up with something like this:

Vector3 dir3 = new Vector3( -$$anonymous$$athf.Sin($$anonymous$$athf.Deg2Rad horzAngle), $$anonymous$$athf.Sin($$anonymous$$athf.Deg2Rad vertAngle), $$anonymous$$athf.Cos($$anonymous$$athf.Deg2Rad * horzAngle) );

it works with the horizontal change, and also with the vertical; but only to 45deg up or down.

Problem is that the Z value needs to be used by both the horizontal and the vertical.

2 Replies

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

Answer by Bunny83 · Aug 01, 2015 at 07:15 PM

To convert polar coordinates to a cartesian coordinates you just need to do this:

 public static Vector3 GetDirection(float aAzimuth, float aAltitude)
 {
     aAzimuth *= Mathf.Deg2Rad;
     aAltitude *= Mathf.Deg2Rad;
     float c = Mathf.Cos(aAltitude);
     return new Vector3(Mathf.Sin(aAzimuth) * c, Mathf.Sin(aAltitude), Mathf.Cos(aAzimuth) * c);
 }

The Azimuth (heading) specifies the angle around they y axis [-180°, 180°]. The Altitude is the elevation angle which is in the range [-90°,90°].

However you can also simply use Unity's Quaternion type like this:

 var dir = Quaternion.Euler(azimuth, altitude, 0) * Vector3.forward;
Comment
Add comment · Show 3 · 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 nullgobz · Aug 01, 2015 at 11:40 PM 0
Share

Bunny you are right this is the more correct awnser! But i dident set the awnser as accepted he did so cant change it. Thx man!

avatar image fafase · Aug 02, 2015 at 09:37 AM 0
Share

I did set it as answer since you said it was the solution (It was originally a comment). I changed that.

I leave $$anonymous$$e though as I would think some people may find your question while their problem can be solved with cross product (semantic issue).

avatar image nullgobz · Aug 02, 2015 at 01:40 PM 0
Share

fafase thx for not removing yours. Still like to have it for reference.

avatar image
0

Answer by fafase · Aug 01, 2015 at 06:41 PM

Are you looking for the cross product?

The result of the cross product of two vectors is another vector that is perpendicular to the plane on which the two vectors lie.

Based on the order of the parameter, the result is going up or down as shown on the picture.

alt text

Comment
Add comment · Show 4 · 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 nullgobz · Aug 01, 2015 at 06:58 PM 0
Share

fafase YES! it works! thx so much!!

avatar image Bunny83 · Aug 01, 2015 at 07:23 PM 0
Share

Huh? I thought the question asks for something different ... If you create two vectors like stated in the question, if both angles are 0 both vectors will point in the same direction. The cross product would return a null vector in that case.

I don't see how the cross product has any use in this case ^^.

avatar image nullgobz · Aug 01, 2015 at 11:32 PM 0
Share

yeh true, but thay would never both be 0 in my case

avatar image nullgobz · Aug 01, 2015 at 11:41 PM 0
Share

the cross product did work, i dident think there where any other solution.

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

25 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

Related Questions

Set rotation based on 2 points problem 1 Answer

3rd person unit control 0 Answers

How to add slight angle/rotation variation to transform.forward? 5 Answers

AI movement with rigidbody2d 0 Answers

How to edit Direction Angle using GetAxis 2.5D 8 direction movement 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