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 highpockets · Feb 05, 2014 at 11:31 PM · rotationmeshpositionverticesalignment

How to find the closest 4 verts/corners ( along with their orientation ) of a rectangular object in relationship with another object?

Hi,

In my game, the player object picks up and drops rectangular objects ( rects ). Before the player is allowed to pick up one of these rects, the rects need to have one face parallel with the x/z. If one face of the rect is parallel with the x/z then the player can pick up the object, but as the player moves into position, it needs to rotate on the y axes and extend or retract it's arms so the player object's corners align with the closest 4 corners/verts of the rect ( highest verts on the y axes ).

For finding out if the rects are sitting flat with one face parallel with the x/z axes, renderer.bounds.size.y seems to give good results because the rects are 3x3x6, so it will give me 3 or 6 if it is flat. Please let me know if that is bad practice. For finding out the closest 4 corners/verts and the y rotation that the player object would have to assume to line up with the 4 verts, I was thinking of using mesh.vertices to find the certs with the highest y value and calculating the angle between them to attain the y rotation needed for alignment, but is there a more standard way to do something like this without making extra child game objects for each corner of the rects??

Comment
Add comment · Show 2
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 highpockets · Feb 05, 2014 at 11:46 PM 0
Share

oops, mesh.vertexCount is 24, I guess I'll have to find the closest 12 verts. Open to ideas, as I'm sure there are better approaches.

avatar image Scribe · Feb 05, 2014 at 11:54 PM 0
Share

Are the shapes always perfect rectangles, if so you could check that the x and y rotations are multiples of 90? I'm not sure I've completely grasped the situation of your problem.

1 Reply

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

Answer by Scribe · Feb 06, 2014 at 12:37 AM

Hi,

I think I've found quite a good way of doing this using normals:

 var mesh : Mesh;
 var normals : Vector3[];
 
 function Start(){
     mesh = GetComponent(MeshFilter).mesh;
     normals = mesh.normals;
 }
 
 function Update () {
     for (var i = 0; i < normals.Length; i++){
         var V = transform.TransformDirection(normals[i]).normalized;
         if(Mathf.Abs(V.y) == 1){
             // if it gets here then there is a face that faces along the y-axis
             break;
         }
     }
 }

Currently this is set up to work if it is attached to your 'rects' so it may need to be changed for your situation, but it should be reliable and should be more efficient than searching through vertices (as all 3D objects have more vertices than faces).

Scribe

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 highpockets · Feb 06, 2014 at 12:58 AM 0
Share

It's strange, I originally found that the rotation was tough to find, but it seems that the y rotation of the rects is giving consistent results regardless of the rotation on the other axes. When I first tested it in the editor during runtime, I was rotating y and z and it was just showing the y axes rotation. I guess I have to study euler angles more. As long as one face is pointing along the y axis, the euler y rotation is giving me what I need.

Thanks for what you have done above, although do you think that it would be more efficient than renderer.bounds.size.y??

avatar image Scribe · Feb 06, 2014 at 04:20 AM 0
Share

Glad you got it working, honestly I have no idea about this options efficiency in comparison to renderer.bounds though you can test it quite easily by instantiating a few thousand cubes with each variation of the script attached.

avatar image highpockets · Feb 06, 2014 at 04:31 AM 0
Share

right, thanks Scribe

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

19 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

Related Questions

Generate mesh from raycast positions, independent of rotations 0 Answers

Changing vertex positions of a uGUI Text Mesh 0 Answers

Anchor TextMesh position to objects. 0 Answers

UnityEngine.UI.Text characters mesh 0 Answers

Editing vertices 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