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 Jota_sk · Aug 16, 2020 at 11:45 AM · directionvector

Clamp Vector direction to one axis

Hello, I'm struggling a bit with vector3 operations. I have a rectangle (called A) and inside that rectangle a set of cubes (let's call each cube B) can be placed in the border of the rectangle A. I have managed to get the directoin of the border rectangle for the room (simple dir = (b.pos - a.pos).normalized). Here is what i got at the moment: alt text

The black dot is the A rectangle position, and the purple lines it's the direction each B square is facing from the rectangle A. But what i want it's a Vector direction like the red arrow, where each direction is clamped/align to one axis only, so we know which part of the rectangle is placed. I need this to be programatically as in a later stage i want to add many different shapes of A and i need to know the face they are placed.

 var dir = (d.transform.position - r.transform.position).normalized * 5f;
 Debug.DrawLine(d.transform.position, d.transform.position + dir, Color.magenta);

example.png (16.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 Bunny83 · Aug 16, 2020 at 01:07 PM

At no point have you included the dimensions of your rectangle, so anything you do won't work without those information. The key is simply to figure out on which side of the rectangle you are. This can easily be done by first scaling space so the rectangle becomes a square. This can be easily done with the aspect ratio of your rectangle. Once you have the positions of your objects in the "fixed" space, it's just a matter of some nested if statements. If the absolute value of the x coordinate is greater than the absolute value of the y coordinate, the normal is either the right or left vector, if it's the other way round the normal would be either the up or down vector. By looking at the sign of the components you can determine which case actually applies. This trivial case of course only applies for an axis aligned rectangle. However even with a rotated one, if you have the rotation quaternion available it's not that much harder.


 public static Vector3 GetRectangleNormal(Vector3 aRectCenter, Vector2 aRectSize, Quaternion aRectRot, Vector3 aPosition)
 {
     Vector3 vec = aPosition - aRectCenter;
     vec = Quaternion.Inverse(aRectRot) * vec;
     vec.y *= aRectSize.x / aRectSize.y;
     Vector3 normal;
     if (Mathf.Abs(vec.x) > Mathf.Abs(vec.y))
         normal = Vector3.right * Mathf.Sign(vec.x);
     else
         normal = Vector3.up * Mathf.Sign(vec.y);
     return aRectRot * normal;
 }


To break it down

 // worldspace direction from the rect center to the position
 Vector3 vec = aPosition - aRectCenter;
 
 // if the rectangle is rotated, apply the inverse to get a local space direction
 vec = Quaternion.Inverse(aRectRot) * vec;

 // make space square by fixing with the aspect ratio.
 vec.y *= aRectSize.x / aRectSize.y;
 
 // the x component is greater than the y component, we are on one of the sides (left or right)
 // otherwise we are on the top or bottom edge
 if (Mathf.Abs(vec.x) > Mathf.Abs(vec.y))

 // in case we are on one of the sides, thus the localspace normal is simply (1,0,0). We multiply
 // by the sign of the x component to get (-1,0,0) if it's on the left side
 normal = Vector3.right * Mathf.Sign(vec.x);

 // same logic as above but this time the normal is (0,1,0) and the sign of the y component
 // decides if it's up or down.
 normal = Vector3.up * Mathf.Sign(vec.y);

 // finally we rotate our local space normal into worldspace
 return aRectRot * normal;

This should work for any rectangle that is defined in the local x-y-space with an arbitrary 3d rotation.

Comment
Add comment · Show 1 · 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 · Aug 16, 2020 at 01:21 PM 0
Share

If you don't need the rotation, you can simply remove the quaternion and remove the line 4 completely and at the end just return normal.

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

135 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

Related Questions

Move point in the direction of another 1 Answer

Need to know euler angles of Vector3 direction relative to other Vector3 direction 1 Answer

How should I rotate a 2D unit vector by X degrees? 1 Answer

Vector direction after collision 1 Answer

Why is my gun shooting backward? 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