Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
4 captures
13 Jun 22 - 14 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 arjenvklaveren · Apr 08 at 12:14 PM · vector3mathtransformation

How to give a position an offset relative to its forward vector in local space?

In my road creation tool, I want to create a road out of 2 points. In order to do that I need to create a box of vertexes where 4 points surround the 2 initial points. However, I am struggling to find out where to place those points when the road is at an angle. I have the feeling it might be something really easy but my brain is just not cathcing it.

The difference in the example is really small but I hope it makes clear what I am trying to achieve. if you need any initial information feel free to ask but I hope the illustration speaks for itself.

alt text

example.png (85.5 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
1
Best Answer

Answer by Captain_Pineapple · Apr 08 at 01:04 PM

Yes that illustration is enough.

So if we assume you have a starting point and endpoint and an offset:

  Vector2 startpoint = new Vector2(x1, y1); 
  Vector2 endpoint = new Vector2(x2, y2); 
  float offset = 3;

Then you can calulate the following vectors:

   //connect the red dots, get the vector and shorten it to length 1:
   Vector2 direction_normalized = (endpoint - startpoint).normalized;
   // get the perpendicular vector: (normalization might not be needed here - not sure):
   Vector2 direction_perpendicular = Vector2.perpendicular(direction_normalized).normalized;

Then the 4 points you are searching for are:

   Vector2 P1 = startpoint + offset*(-direction_normalized  + direction_perpendicular);
   Vector2 P2 = startpoint + offset*(-direction_normalized  - direction_perpendicular);

   Vector2 P3 = endpoint + offset*(direction_normalized  - direction_perpendicular);
   Vector2 P4 = endpoint + offset*(direction_normalized  - direction_perpendicular);

This can ofc also be done in 3D space. Only thing to change in that regard is that 3D space does not contain a simple "perpendicular" function. There you'd have to learn to use the cross product. (`Vector3.cross(v1, v2)`)

Let me know if something was unclear.

Comment
Add comment · Show 2 · 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 arjenvklaveren · Apr 08 at 01:25 PM 0
Share

That's such an elegant solution! I was just trying stuff like rotating the forward vector by 45 degrees and stuff but this makes way more sense. Thanks!

avatar image Captain_Pineapple arjenvklaveren · Apr 08 at 02:42 PM 0
Share

Doing this with rotations is also very much a possible solution! As this would be more complex and more taxing regarding performance the pure vector calculations are more favorable for a real application though.

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

175 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 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

Make something move away from click point. 2 Answers

Constant total time over multiple Lerp functions 4 Answers

managing floating point rounding 1 Answer

How to create 2 vectors parallel to a plane? or Parallel to Hit.point? For hand IK placement 0 Answers

Extending a vector? 4 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