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 wolga2 · Apr 23, 2014 at 01:55 AM · rotationmeshspawnvertices

Orientate Planes created on vertices towards mehs they're spawned on

Hi! I spawn small planes on a huge mesh by writing the following:

 var maxAmount : int = 10;
 var mesh : Mesh = GetComponent(MeshFilter).mesh;
     var vertices = mesh.vertices;
     for (var i = 0; i < maxAmount; i++)
     {
         var someNum = Random.Range (0, vertices.length);
         var spawnPoint = transform.TransformPoint(vertices[someNum]);
         plane = GameObject.CreatePrimitive(PrimitiveType.Plane);
         plane.transform.position = spawnPoint;

Thus i don't define any rotation for the these created planes, they clearly all face in the same direction. The mesh on which they're spawned on looks like a rock, the normals look in many different directions. How can i make the planes rotate at creation in a way towards the mehs that it looks like as they would lie on it?

Thanks in advance!

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 robertbu · Apr 23, 2014 at 02:04 AM

I assume you want the planes to face away from the 'rock' so that you can see the surface of the plane. Vertices do not define normals...the triangles do. From what you are doing, it would be better to process each triangle and place the plane on the center of the triangle with the normal of the plane aligned with the normal of the triangle. The Mesh.normals supply the normal for each triangle, though you will need to do a Transform.TransformDirection() on it.

If you pivot point is in the center of your 'rock', then your code with something like this may be good enough:

 var dir = spawnPoint - transform.position;
 plane.transform.rotation = Quaternion.FromToRotation(Vector3.up, dir);

Note if you are going to be creating a lot of these, then you may not want to use the built-in plane. It has far more triangles than is necessary. You can use the CreatePlane editor script from the Wiki to make a two triangle, horizontal plane, or you could use a Quad and change the code I've outline to align Vector3.back with 'dir'.

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 wolga2 · Apr 24, 2014 at 12:53 AM 1
Share

thank you very for your answer! you were right about the triangles. i could solve it by placing the plane on a vertice of a random triangle on the mesh and then rotate it away from it by the direction of the triangles normal. here's my code: var maxAmount : int = 10; var mesh : $$anonymous$$esh = GetComponent($$anonymous$$eshFilter).mesh; var vertices = mesh.vertices; var normals = mesh.normals; var triangles = mesh.triangles;

 for (var i = 0; i < maxAmount; i++)
     {
         var someNum = Random.Range (0, triangles.length);
         var vert = vertices[triangles[someNum]];
         var spawnPoint = transform.TransformPoint(vert);
         var plane = Instantiate (prefPlane, spawnPoint, Quaternion.identity);
         var dir = normals[triangles[someNum]];
         plane.transform.rotation = Quaternion.FromToRotation(Vector3.up, dir);
 }

and thanks for the tipp with the built-in plane :) i created one with the suggested createplane editor script with only two triangles and use it as the prefab 'plane' to instantiate it.

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

20 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

Related Questions

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

Generate mesh from raycast positions, independent of rotations 0 Answers

How can i get current state(position) of vertices of animated mesh(SkinnedMeshRenderer)? 4 Answers

Can't change verts on mesh after assigning to meshfilter. 1 Answer

Calculating mesh over GameObject scale 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