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
0
Question by ZorNiFieD · Dec 09, 2012 at 08:22 AM · 2drotationvector3angle

Calculate third Vector3 with two Vector3s and an Angle

Greetings;

I'm trying to accomplish the finding myPointC, see comments to see where I need help with. The rest of the code works as intended. The function I'm having a problem with is how to find using an angle. The line length does not even need to be here as it's not really an issue because I know that value.

the basic is find pointC relative to pointA using pointA, pointB and an Angle.

Here is a image for a visual question if this can help you any. I'm pulling my hair out over this, even though I'm already bald:

alt text

 private void DrawLineUsingDistanceAtAngle(){
         Vector3 myPointA;
         Vector3 myPointB;
         Vector3 myPointC;
         float lineAngle = 15f;
         float lineLength = 10f;
         myPointB = LerpByDistance(myPointA, myPointB, lineLength);
         myPointB = GetThirdVector3UsingAngle(
                 myPointA, myPointB, lineAngle, "x");
         DoSomethingGreat(myPointA, myPointB);
     }
     
     private Vector3 GetThirdVector3UsingAngle(
             Vector3 pointA, Vector3 pointB, 
             float angle, string zeroAxis = "z")
     {
         Vector3 pointC;
         if (zeroAxis == "x"){
             pointA.x = 0;
             pointB.x = 0;
         }else if(zeroAxis == "y"){
             pointA.y = 0;
             pointA.y = 0;
         }else{
             pointA.z = 0;
             pointA.z = 0;
         }
         // do calculation to get pointC here
                 
         return pointC;
     }
     
     public Vector3 LerpByDistance(Vector3 A, Vector3 B, float x)
     {
         Vector3 P = x * Vector3.Normalize(B - A) + A;
         return P;
     }


findpointc.png (19.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

1 Reply

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

Answer by Owen-Reynolds · Dec 09, 2012 at 05:58 PM

Take that top line AB, rotate it by the angle, then rescale for the correct length. I'm going to assume this is a top view, so the rotation is over Y. I think you have it all except the actual rotate, but including it all for context:

 Vector3 B2 = B-A; // move line AB to come out of (0,0,0) so rotate is easier
 Quaternion angleSpin = Quaternion.Euler(0,angle,0); // create a rotation out of angle
 Vector3 C2 = angleSpin * B2; // rotate AB by the angle

 // C2 is now facing the correct way, but coming out of (0,0,0) and the wrong len:
 C = C2.normalized * cLen + B; // rescale, recenter coming out of A
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 Bunny83 · Dec 09, 2012 at 07:19 PM 1
Share

The answer is correct (except you should add A, not B) but as you said you need two more things bedside the angle and the other two points:

  • the rotation axis

  • the length of AC

otherwise with only the information in the original post, point C could be anywhere on a cone surface.

avatar image ZorNiFieD · Dec 09, 2012 at 09:13 PM 0
Share

This still isnt working for me, can you kindly review my edited code below? I'm still getting a straight line to the original B point. Also; I already have a LerpByDistance function which moves the point down a line between two points so I believe that solved the .Normalized section of the original answer.

 private Vector3 GetThirdVector3UsingAngle(Vector3 A, Vector3 B, float angle, float cLen, string rotateOnAxis = "y")
     {
         Vector3 B2 = B - A; // move line AB to come out of (0,0,0) so rotate is easier
         Quaternion angleSpin;
         if (rotateOnAxis == "x"){
             angleSpin = Quaternion.Euler(angle,0,0); // create a rotation out of angle on x axis
         }else if(rotateOnAxis == "z"){
             angleSpin = Quaternion.Euler(0,0,angle); // create a rotation out of angle on z axis
         }else{
             angleSpin = Quaternion.Euler(0,angle,0); // create a rotation out of angle on y axis default
     }
         Vector3 C2 = angleSpin * B2; // rotate AB by the angle
         C2 = LerpByDistance(A, B, cLen); // rescale, recenter co$$anonymous$$g out of A
         return C2;
     }

 public Vector3 LerpByDistance(Vector3 A, Vector3 B, float x)
     {
         // x is distance from point A that you want to travel
         Vector3 P = x * Vector3.Normalize(B - A) + A;
         return P;
     }
avatar image ZorNiFieD · Dec 10, 2012 at 01:46 AM 0
Share

$$anonymous$$y apologies... The above code is working as the question asks. Some other portion of my program was off so I prematurely stated it wasn't working. Both functions do as intended as I wrote them above. Thank you both for your help in this. Now my 2 day search on solving this matter while pulling my hair out is closed.

Thanks!

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

11 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

Related Questions

How do I find the angle between the players forward vector and the mouse position? 1 Answer

Get Direction from 2 Vectors - and Apply to Transform 0 Answers

How to use Quaternion to rotate an object in only one axis? 1 Answer

Firing a projectile in 2D 0 Answers

Moving by rotation of objects 0 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