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 cbetsikos · Jan 29 at 07:01 PM · 2d gamecollider2dnot workingpolygon collider 2d

Polygon Collider 2d doesnt work properly

Hello (and sorry for my bad English), I want to make a game with 2d Planets. I have managed to create a Mesh, but the Polygon Collider 2d doesnt work properly. Most sides of the Planet collision are working, but on a few sides the Player is falling through the Planet. alt text

 using UnityEngine;
 
 public class CircleMesh : MonoBehaviour
 
 {
     Mesh mesh;
     public PolygonCollider2D polygonCollider;
     Vector3[] vertPos;
     Vector2[] vert2dPos;
     public int vertecies = 20;
     public float radiusA = 20;
     public float radiusB = 20;
 
     Vector3[] verticies;
     int[] triangle;
     private void Start()
     {
         vertPos = new Vector3[vertecies + 1];
         vert2dPos = new Vector2[vertecies];
         triangle = new int[vertecies * 3];
         mesh = new Mesh();
         GetComponent<MeshFilter>().mesh = mesh;
         vertPos = CalculateVerticies(vertecies, radiusA, radiusB);
 
         for (int i = 0, j = 1; i <= triangle.Length - 3; i += 3)
         {
             triangle[i] = j;
             triangle[i + 1] = 0;
             j++;
             if (i == triangle.Length - 3)
             {
                 triangle[i + 2] = 1;
             }
             else
             {
                 triangle[i + 2] = j;
             }
         }
         polygonCollider.pathCount = vertecies;
         for (int i = 0; i < vert2dPos.Length; i++)
         {
             vert2dPos[i] = vertPos[i + 1];
             polygonCollider.SetPath(i, vert2dPos);
         }
         for (int i = 0; i < vert2dPos.Length; i++)
         {
             Debug.Log(vert2dPos[i]);
 
         }
 
         mesh.vertices = vertPos;
         mesh.triangles = triangle;
 
         // polygonCollider.points = vert2d;
 
     }
 
     Vector3[] CalculateVerticies(int corners, float radiusA, float radiusB)
     {
         float angle = (2 * Mathf.PI) / corners;
         Vector3[] verticies = new Vector3[corners + 1];
         float currentAngle = 0;
         verticies[0] = new Vector3(0, 0, 0);
         for (int i = 0; i < corners; i++)
         {
             currentAngle = currentAngle + angle;
             float xPos = Mathf.Cos(currentAngle) * radiusA * Random.Range(1, 1.2f);
             float yPos = Mathf.Sin(currentAngle) * radiusB * Random.Range(1, 1.2f);
             Vector3 vertice = new Vector3(xPos, yPos, 0);
             verticies[i + 1] = vertice;
         }
         return verticies;
 
     }
 }
 

Can someone help me........?

unityhelp.png (11.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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by privatecontractor · Jan 30 at 07:49 PM

Hi @cbetsikos,

Code and result look awesome, did you set Continuous collision detection on Rigidbody component atached to player?

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 cbetsikos · Jan 31 at 07:43 PM 0
Share

I think the problem is in the Code.

avatar image
0

Answer by cbetsikos · Jan 30 at 08:39 PM

Hello @privatecontractor, thank you. Yes I have set Continuous collision detection on the Rigidbody. I think the problem is in the Code, because when I use polygon.points instead of polygon.Setpath the collision works fine. But the collider doesnt fit perfectly to the mesh. Here is what I mean: alt text

and here the Code:

 using UnityEngine;
 
 public class CircleMesh : MonoBehaviour
 
 {
     Mesh mesh;
     public PolygonCollider2D polygonCollider;
     Vector3[] vertPos;
     Vector2[] vert2dPos;
     public int vertecies = 20;
     public float radiusA = 20;
     public float radiusB = 20;
 
     Vector3[] verticies;
     int[] triangle;
     private void Start()
     {
         vertPos = new Vector3[vertecies + 1];
         vert2dPos = new Vector2[vertecies];
         triangle = new int[vertecies * 3];
         mesh = new Mesh();
         GetComponent<MeshFilter>().mesh = mesh;
         vertPos = CalculateVerticies(vertecies, radiusA, radiusB);
 
         for (int i = 0, j = 1; i <= triangle.Length - 3; i += 3)
         {
             triangle[i] = j;
             triangle[i + 1] = 0;
             j++;
             if (i == triangle.Length - 3)
             {
                 triangle[i + 2] = 1;
             }
             else
             {
                 triangle[i + 2] = j;
             }
         }
        //polygonCollider.pathCount = vertecies;
         for (int i = 0; i < vert2dPos.Length; i++)
         {
             vert2dPos[i] = vertPos[i + 1];
             //polygonCollider.SetPath(i, vert2dPos);
         }
         for (int i = 0; i < vert2dPos.Length; i++)
         {
             Debug.Log(vert2dPos[i]);
 
         }
 
         mesh.vertices = vertPos;
         mesh.triangles = triangle;
 
          polygonCollider.points = vert2dPos;
 
     }
 
     Vector3[] CalculateVerticies(int corners, float radiusA, float radiusB)
     {
         float angle = (2 * Mathf.PI) / corners;
         Vector3[] verticies = new Vector3[corners + 1];
         float currentAngle = 0;
         verticies[0] = new Vector3(0, 0, 0);
         for (int i = 0; i < corners; i++)
         {
             currentAngle = currentAngle + angle;
             float xPos = Mathf.Cos(currentAngle) * radiusA * Random.Range(1, 1.5f);
             float yPos = Mathf.Sin(currentAngle) * radiusB * Random.Range(1, 1.5f);
             Vector3 vertice = new Vector3(xPos, yPos, 0);
             verticies[i + 1] = vertice;
         }
         return verticies;
 
     }
 }
 


unityhelp2.png (10.9 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
avatar image
0

Answer by Madisen54 · Feb 03 at 05:46 PM

you should check Rigidbody Component that attached to objects because Collision need physics component(if game was 2d you should use RigidBody2D) and disable isKinematic because Collision don't work

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 cbetsikos · Feb 04 at 05:06 PM 0
Share

It isnt the rigidbody (read my comment)

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

184 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 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

How to get OnPointerClick working with a CompositeCollider2D? 0 Answers

Double Jump 1 Answer

How do i stop rigidbody2D bounce 1 Answer

Detect for every side of a polygon which way it is facing. 1 Answer

How to enable/disable polygoncollider2d using script? 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