• 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 vmnoodel · Jan 07, 2015 at 07:59 PM · anglevectorfind

finding position of second vector when first vector and angle between two vectors is given

Hello.

I need to find the value of second vector by C# code when the first vector is given

e.g.

 Vector2 v1 = new Vector2(0,0); // known
 Vector2 v2 ; //unknown
 

Angle between v1 and v2 = 33 Degree find the value of v2

I have the following formula for the same , but I am not able to convert it into a C#

c = cos(theta); s = sin(theta); n = norm(u);

v1 = [u(1)*c-u(2)*s,u(2)*c+u(1)*s]/n; v2 = [u(1)*c+u(2)*s,u(2)*c-u(1)*s]/n;

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 Ed unity · Jan 07, 2015 at 08:15 PM 0
Share

Dan Greene's suggested solution is much easier to implement, but if you want to convert your formulas into c# code, I suggest using the .net $$anonymous$$ath class which includes calls for sin, cos.

avatar image Owen-Reynolds · Jan 07, 2015 at 09:18 PM 0
Share

Is everything 2D? If so, xy (wall facing you) or xz (the ground of a 3D game)? You can also go in either direction (DanG's code spins counter-clockwise(?) in xy.)

In 3D, you have a ton more options (can spin in any diagonal.)

avatar image vmnoodel · Jan 08, 2015 at 06:53 AM 0
Share

that was awesome one line solution Dan !!!

3 Replies

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

Answer by Victory Dan Greene · Jan 07, 2015 at 08:07 PM

You could create a Quaternion from the given angle, then multiply it by v1 to get v2:

 v2 = Quaternion.AngleAxis( theta, Vector3.forward ) * v1 ;
Comment
Add comment · 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
0

Answer by jpthek9 · Jan 07, 2015 at 08:20 PM

Without the distance, you can't find the 2nd vector but you can find the normalized vector.

Using the law of sines and assuming that the hypotenuse of the right-triangle we have length 1, we now have a simple AAS triangle that we can solve with the law of sines. v2.y (the opposite side) = sin(angle)/sin(90) and v2.x (the adjacent side) = sin(90-angle)/sin(90).

In code, that's:

 angle = 33;
 v1 = Vector2.zero;
 v2.x = Mathf.sin(90-angle);
 v2.y = Mathf.sin(angle);
 v2 /= Mathf.sin(90);
 

Then just add your first vector to v2 and you have a point on the ray created by 33 degrees from your first vector.

 v2 += v1;

Update: Code slightly optimized.

Comment
Add comment · 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
0

Answer by Scribe · Jan 07, 2015 at 08:23 PM

using trig as your formula suggests you would like, something like the following should work:

 public Vector2 v1;
 public float degrees = 0;
 float v1Degrees = 0;
 Vector2 v2;
 
 void Update(){
     Debug.DrawRay(Vector3.zero, v1);
     Debug.DrawRay(Vector3.zero, v2.normalized, Color.blue);
     
     v1Degrees = Mathf.Atan2(v1.x, v1.y);
     degrees = Mathf.Repeat(degrees, 360);
     v1Degrees += degrees*Mathf.Deg2Rad;
     v1Degrees = Mathf.Repeat(v1Degrees+Mathf.PI, 2*Mathf.PI)-Mathf.PI;
     v2 = new Vector2(Mathf.Tan(v1Degrees), 1);
     
     if(v1Degrees <= -Mathf.PI/2 || v1Degrees >= Mathf.PI/2){
         v2 *= -1;
     }
 }

however Dan's method in the comments is much neater, though you could set this up to be in a nice method call!

Comment
Add comment · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do you find one Vector's position of an Array? 1 Answer

Does unity implement a way to convert an angle+magnitude representation of a vector to an x,y vector? 1 Answer

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

Calculating the Angle between two vectors 2 Answers

How to Get 360 degree of angle while dragging object 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