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 daevee · Jun 06, 2014 at 10:03 AM · 2dcollisionterrainmeshvector2

Having an array of points(vector2), how do I create a 2d terrain? (new to unity)

I'm new to unity and i don't know what to look for to obtain what i need. So basically this what i have alt text

The round thingie is my rigidbody with polygon collider. The lines you see are generated from an array of points and drawn with debug.SrawLine at the moment. What i need to get is this : alt text

What "thing" do I have to do to make this and make it a rigid body? Terrain? Meshes? Polygons? It's not very clear to me how it should work even if i did my researches. Of course those lines will need to be a closed perimeter, at the least i think. But that's not a problem.

immagine1.jpg (20.9 kB)
immagine2.jpg (54.8 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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by ericksson · Jun 07, 2014 at 10:54 PM

You can use http://docs.unity3d.com/ScriptReference/PolygonCollider2D.SetPath.html

Comment
Add comment · 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
0

Answer by satanas · Jan 19, 2020 at 09:42 AM

Probably a little too late but it could be helpful if someone else comes across the same problem. This is how I solved it. First, make sure each of your shapes (in your case top and bottom) have attached:

  • A MeshRenderer with Sprite-Default as material

  • A PolygonCollider2D (empty)

  • A MeshFilter

Then create a C# script with the code below and attach it to the game object:

 public class Landscape : MonoBehaviour {
 
     private MeshFilter meshFilter;
     private PolygonCollider2D polygonCollider;
 
     void Start() {
         meshFilter = GetComponent<MeshFilter>();
         polygonCollider = GetComponent<PolygonCollider2D>();
 
         // Let's assume you have a FooBar method that returns your array of points
         Vector2[] myArrayOfPoints = FooBar();
 
         // We need to convert those Vector2 into Vector3
         Vector3[] vertices3D = System.Array.ConvertAll<Vector2, Vector3>(vertices2D, v => v);
 
         // Then, we need to calculate the indices of each vertex.
         // For that, you can use the Triangulator class available in:
         // http://wiki.unity3d.com/index.php?title=Triangulator
         Triangulator triangulator = new Triangulator(vertices2D);
         int[] indices = triangulator.Triangulate();
 
         // Now we need to create a color for each vertex. I decided to put them all white but you
         // can adjust it and change it according to your needs
         Color[] colors = Enumerable.Range(0, vertices3D.Length)
             .Select(i => Color.white)
             .ToArray();
 
         // Finally, create a Mesh and set vertices, indices and colors
         Mesh mesh = new Mesh();
         mesh.vertices = vertices3D;
         mesh.triangles = indices;
         mesh.colors = colors;
 
         // Recalculate the shape of the mesh
         mesh.RecalculateNormals();
         mesh.RecalculateBounds();
 
         // And add the mesh to your MeshFilter
         meshFilter.mesh = mesh;
 
         // For the collisions, basically you need to add the vertices to your PolygonCollider2D            
         polygonCollider.points = vertices2D;
     }
 }


I tested it with some random generated mountains and you can see how a box is colliding with these mountains in the screenshot below: screenshot


screen-shot-2020-01-19-at-14051-am.png (23.7 kB)
Comment
Add comment · 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

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

23 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

Related Questions

Generate collision mesh from 2d texture? 0 Answers

Terrain Detail Mesh Collisions 1 Answer

Find cardinal direction of collision based on 2D Cartesian coordinates 1 Answer

Crawling around the terrain 3 Answers

Collision with Texture when alpha=1 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