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 podentag · May 07, 2014 at 12:21 PM · vector3linerenderercurvebezier

Find Vector3 point exactly between two gameobjects with an offset perpendicular to the line connecting the two gameobjects

I am using a linerenderer (called line) to create a bezier curve between two gameobjects like this (C#):

         float t; 
         Vector3 position;
         for(int ii = 0; ii < 32 /* amount of line segments */; ii++)
         {
             t = ii / (32F - 1F);
             position = (1F - t) * (1F - t) * object1.transform.position 
                 + 2F * (1F - t) * t * Vector3.zero
                     + t * t * object2.transform.position;
             line.SetPosition(ii, position);
         }

Right now, the control point is Vector3.zero. I want the control point to be exactly between the two gameobjects with an offset perpendicular to the line connecting the two gameobjects. A quick sketch included in this question shows what I want to achieve:

[1]: /storage/temp/26296-controlpointbezier.png

When I would try something like this in a 2d setting, I would have used some simple trigonometry, but I'm quite new to 3d graphics and am not sure how to work this out.

controlpointbezier.png (2.6 kB)
Comment
Add comment · Show 1
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 Owen-Reynolds · May 07, 2014 at 02:05 PM 1
Share

As Lootnitik notes in the comment, in 3D space, "perpendicular to a line" could be lots of lines. You have to decide "perpendicular to what else"? Like to the camera, or the first object's facing. And even then it can go left or right.

It's not really 3D graphics -- just 3D geometry.

1 Reply

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

Answer by NickP_2 · May 07, 2014 at 12:51 PM

The midpoint between 2 vectors is as easy as (vector1 + vector2)/2. Add vector.up (if it needs to be on the y axis) times a certain amount. Hope it helps

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 podentag · May 07, 2014 at 01:05 PM 0
Share

Finding the midpoint works, thanks! The vector.up works on the y-axis indeed, but I want to place the control point with an offset perpendicular to the line connecting the two gameobjects, this is the most difficult part I think...

avatar image Lo0NuhtiK · May 07, 2014 at 01:47 PM 2
Share

Try integrating something like this into your code.

 Transform t1 = someTrans ;
 Transform t2 = someOtherTrans ;
 
 float offset = someFloatValue ;
 
 Vector3 GetPoint()
 {
    //get the positions of our transforms
    Vector3 pos1 = t1.position ;
    Vector3 pos2 = t2.position ;
    
    //get the direction between the two transforms -->
    Vector3 dir = (pos2 - pos1).normalized ;
 
    //get a direction that crosses our [dir] direction
    //NOTE! : this can be any of a buhgillion directions that cross our [dir] in 3D space
    //To alter which direction we're crossing in, assign another directional value to the 2nd parameter
    Vector3 perpDir = Vector3.Cross(dir, Vector3.right) ;
 
    //get our midway point
    Vector3 midPoint = (pos1 + pos2) / 2f ;
 
    //get the offset point
    //This is the point you're looking for.
    Vector3 offsetPoint = midPoint + (perpDir * offset) ;

    return offsetPoint ;
 }

 void Some$$anonymous$$ethodToDoStuff()
 {
    Vector3 point = GetPoint() ;

    //do whatever else with the point now...
 }
avatar image robertbu · May 07, 2014 at 03:08 PM 0
Share

If the drawing is 2D on the XY plane, then Vector3.forward rathar than Vector3.right for the Cross().

avatar image podentag · May 07, 2014 at 05:28 PM 0
Share

Great, I've got it all working. Thanks a lot!

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

24 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

Related Questions

Bezier movement is erratic 1 Answer

Bezier curver using LineRenderer 0 Answers

how to find a point(vector2) between two points(Vector2) of a line (line renderer) 2 Answers

Finding the next point on a Bezier Curve with Vector3's 0 Answers

[Vectrosity] 3d bezier curve not generated correctly 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