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 BanzaiPipeline · Mar 26, 2014 at 01:14 AM · colliderpolygon collider 2d

Modify this script for PolygonCollider2D

Hi guys, I am implementing a Tile Map, from Tiled Map generator using XuniTMX2D from here https://bitbucket.org/Chaoseiro/x-unitmx/overview, i managed to modify the scripts to implement boxCollider2D instead of boxCollider but i have tried to change the method of generating Polygon collider mesh into PolygonCollider2D and It has not work, this is the original code, could someone help me transforming it into PolygonCollider2D? thanks in advance.

 /// <summary>
         /// Generate a Polygon collider mesh
         /// </summary>
         /// <param name="obj">Object which properties will be used to generate this collider.</param>
         /// <param name="zDepth">Z Depth of the collider.</param>
         /// <param name="colliderWidth">Width of the collider.</param>
         /// <param name="innerCollision">If true, calculate normals facing the center of the collider (inside collisions), else, outside collisions.</param>
         /// <returns>Generated Game Object containing the Collider.</returns>
         public GameObject GeneratePolygonCollider(MapObject obj, float zDepth = 0, float colliderWidth = 1.0f, bool innerCollision = false)
         {
             GameObject polygonCollider = new GameObject(obj.Name);
             polygonCollider.transform.parent = this.Parent.transform;
 
             Mesh colliderMesh = new Mesh();
             colliderMesh.name = "Collider_" + obj.Name;
             MeshCollider mc = polygonCollider.AddComponent<MeshCollider>();
 
             List<Vector3> vertices = new List<Vector3>();
             List<int> triangles = new List<int>();
 
             Vector3 firstPoint = (Vector3)obj.Points[0];
             Vector3 secondPoint, firstFront, firstBack, secondFront, secondBack;
             for (int i = 1; i < obj.Points.Count; i++)
             {
                 secondPoint = (Vector3)obj.Points[i];
                 firstFront = new Vector3(obj.Bounds.center.x + firstPoint.x, -obj.Bounds.center.y - firstPoint.y, zDepth - colliderWidth);
                 firstBack = new Vector3(obj.Bounds.center.x + firstPoint.x, -obj.Bounds.center.y - firstPoint.y, zDepth + colliderWidth);
                 secondFront = new Vector3(obj.Bounds.center.x + secondPoint.x, -obj.Bounds.center.y - secondPoint.y, zDepth - colliderWidth);
                 secondBack = new Vector3(obj.Bounds.center.x + secondPoint.x, -obj.Bounds.center.y - secondPoint.y, zDepth + colliderWidth);
                 if (innerCollision)
                 {
                     vertices.Add(firstBack); // 3
                     vertices.Add(firstFront); // 2
                     vertices.Add(secondBack); // 1
                     vertices.Add(secondFront); // 0
                 }
                 else
                 {
                     vertices.Add(firstFront); // 3
                     vertices.Add(firstBack); // 2
                     vertices.Add(secondFront); // 1
                     vertices.Add(secondBack); // 2
                 }
                 triangles.Add((i - 1) * 4 + 3);
                 triangles.Add((i - 1) * 4 + 2);
                 triangles.Add((i - 1) * 4 + 0);
 
                 triangles.Add((i - 1) * 4 + 0);
                 triangles.Add((i - 1) * 4 + 1);
                 triangles.Add((i - 1) * 4 + 3);
 
                 firstPoint = secondPoint;
             }
             // Connect last point with first point
             secondPoint = (Vector3)obj.Points[0];
             firstFront = new Vector3(obj.Bounds.center.x + firstPoint.x, -obj.Bounds.center.y - firstPoint.y, zDepth - colliderWidth);
             firstBack = new Vector3(obj.Bounds.center.x + firstPoint.x, -obj.Bounds.center.y - firstPoint.y, zDepth + colliderWidth);
             secondFront = new Vector3(obj.Bounds.center.x + secondPoint.x, -obj.Bounds.center.y - secondPoint.y, zDepth - colliderWidth);
             secondBack = new Vector3(obj.Bounds.center.x + secondPoint.x, -obj.Bounds.center.y - secondPoint.y, zDepth + colliderWidth);
             if (innerCollision)
             {
                 vertices.Add(firstBack); // 3
                 vertices.Add(firstFront); // 2
                 vertices.Add(secondBack); // 1
                 vertices.Add(secondFront); // 0
             }
             else
             {
                 vertices.Add(firstFront); // 3
                 vertices.Add(firstBack); // 2
                 vertices.Add(secondFront); // 1
                 vertices.Add(secondBack); // 2
             }
 
             triangles.Add((obj.Points.Count - 1) * 4 + 3);
             triangles.Add((obj.Points.Count - 1) * 4 + 2);
             triangles.Add((obj.Points.Count - 1) * 4 + 0);
 
             triangles.Add((obj.Points.Count - 1) * 4 + 0);
             triangles.Add((obj.Points.Count - 1) * 4 + 1);
             triangles.Add((obj.Points.Count - 1) * 4 + 3);
 
             colliderMesh.vertices = vertices.ToArray();
             colliderMesh.triangles = triangles.ToArray();
             colliderMesh.RecalculateNormals();
 
             mc.sharedMesh = colliderMesh;
 
             polygonCollider.isStatic = true;
 
             return polygonCollider;
         }
Comment
Add comment · Show 5
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 Sildaekar · Mar 26, 2014 at 03:26 AM 0
Share

No one here is going to do your work for you, we are simply here to help point you in the right direction.

avatar image Lo0NuhtiK · Mar 26, 2014 at 03:32 AM 0
Share

Depends @Sildaekar , people do get bored or feel like helping a bit more sometimes.

avatar image JasonBricco · Mar 26, 2014 at 03:36 AM 0
Share

He's just asking for help, not to have it done completely for him.

avatar image BanzaiPipeline · Mar 26, 2014 at 09:24 AM 0
Share

Last change but still not working, updated vectors to 2D, removed triangles and added vertices to Polygon path with index 0.

 public GameObject GeneratePolygonCollider ($$anonymous$$apObject obj, float zDepth = 0, float colliderWidth = 1.0f, bool innerCollision = false)
         {
             GameObject polygonCollider = new GameObject (obj.Name);
             polygonCollider.transform.parent = this.Parent.transform;
             PolygonCollider2D pC = polygonCollider.AddComponent<PolygonCollider2D> ();

         List<Vector2> vertices = new List<Vector3> ();
         List<int> triangles = new List<int> ();

         Vector2 firstPoint = (Vector2)obj.Points [0];
         Vector2 secondPoint, firstFront, firstBack, secondFront, secondBack;
         for (int i = 1; i < obj.Points.Count; i++) {
             secondPoint = (Vector2)obj.Points [i];
             firstFront = new Vector2 (obj.Bounds.center.x + firstPoint.x, -obj.Bounds.center.y - firstPoint.y);
             firstBack = new Vector2 (obj.Bounds.center.x + firstPoint.x, -obj.Bounds.center.y - firstPoint.y);
             secondFront = new Vector2 (obj.Bounds.center.x + secondPoint.x, -obj.Bounds.center.y - secondPoint.y);
             secondBack = new Vector2 (obj.Bounds.center.x + secondPoint.x, -obj.Bounds.center.y - secondPoint.y);
             if (innerCollision) {
                 vertices.Add (firstBack); // 3
                 vertices.Add (firstFront); // 2
                 vertices.Add (secondBack); // 1
                 vertices.Add (secondFront); // 0
             } else {
                 vertices.Add (firstFront); // 3
                 vertices.Add (firstBack); // 2
                 vertices.Add (secondFront); // 1
                 vertices.Add (secondBack); // 2
             }
             firstPoint = secondPoint;
         }
         // Connect last point with first point
         secondPoint = (Vector2)obj.Points [0];
         firstFront = new Vector2 (obj.Bounds.center.x + firstPoint.x, -obj.Bounds.center.y - firstPoint.y);
         firstBack = new Vector2 (obj.Bounds.center.x + firstPoint.x, -obj.Bounds.center.y - firstPoint.y);
         secondFront = new Vector2 (obj.Bounds.center.x + secondPoint.x, -obj.Bounds.center.y - secondPoint.y);
         secondBack = new Vector2 (obj.Bounds.center.x + secondPoint.x, -obj.Bounds.center.y - secondPoint.y);
         if (innerCollision) {
             vertices.Add (firstBack); // 3
             vertices.Add (firstFront); // 2
             vertices.Add (secondBack); // 1
             vertices.Add (secondFront); // 0
         } else {
             vertices.Add (firstFront); // 3
             vertices.Add (firstBack); // 2
             vertices.Add (secondFront); // 1
             vertices.Add (secondBack); // 2
         }
         pC.setPath (0, vertices.ToArray ());

         polygonCollider.isStatic = true;

         return polygonCollider;
     }
avatar image BanzaiPipeline · Mar 26, 2014 at 11:01 AM 0
Share

I don't want you to do it for me, I just need a bit of help, thanks in advance.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Fornoreason1000 · Mar 26, 2014 at 09:56 AM

the best way to do these things is first understand the operation of the script and the goal of the script.

in this case your going from 3d collisions to 2d collisions. which means what? No Z axis, so you would rewrite the code so that it no longer takes the z axis into account. but meshes are always in 3d so be wary.

try looking at the differences of what the script is currently doing and what you want it to be doing. for example character turns blue, i want it red, so i find whatever is making him blue and change it so it makes him red.

some key differcnes you should make note of

  • 3d physics uses mesh collides, 2d uses Polygon

  • polygon collision do not accept meshes, they accept an array of verticies

  • 2d vertices shouldn't really have a proper Z axis value

  • http://docs.unity3d.com/Documentation/Components/class-PolygonCollider2D.html

  • this looks useful too http://docs.unity3d.com/Documentation/ScriptReference/PolygonCollider2D.SetPath.html

Hope it helps and good luck let me know if you need anymore help

Comment
Add comment · Show 5 · 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 BanzaiPipeline · Mar 26, 2014 at 11:00 AM 0
Share

Hi thanks a lot for your help, I changed all the Vector3 to Vector2, I also removed Z axis from all the vertices, and finally I added all the vertices to the path of the polygon like this: pC.setPath (0, vertices.ToArray ());

But I haven't used the triangles, I think that I don't need them but I am not really sure about it.

thanks a lot for your time and help!

avatar image BanzaiPipeline · Mar 26, 2014 at 11:01 AM 0
Share

You can see the new updated solution above, I edited it with the new code, so its easier for you to help me :)

avatar image Fornoreason1000 · Mar 26, 2014 at 11:48 AM 0
Share

triangles are specific to meshes, you don't need them. also at line 18, you assign a List to a List , that will cause us problems.

one way you could do this is just set vertices on your object to that collider. since its 2d and has no "pits" and weird shapes.

avatar image BanzaiPipeline · Mar 26, 2014 at 11:53 AM 0
Share

Thanks a lot, I just changed it but it doesn't change much as I was not using the triangles, I am still missing something, It must be something related with vertices index when setting path...

avatar image Fornoreason1000 · May 26, 2014 at 05:37 AM 0
Share

What happening? do you get errors or the collide isnt there or is really messed up?

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

24 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

Related Questions

A node in a childnode? 1 Answer

Combine common surfaces 1 Answer

Box Collider Vs Simple Plane Mesh Collider 1 Answer

Change polygon collider depending on the animation 0 Answers

Cast a ray only against "isvisible" objects 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