Search Unity

Custom Collider

Discussion in 'Physics' started by kitsee, Jul 6, 2015.

  1. kitsee

    kitsee

    Joined:
    Jun 9, 2015
    Posts:
    1
    Hello, i'm currently developing a tile map system and now I'm looking at how other objects will interact with the tile map. I've already created a optional system that will create a polygon collider for each 10 by 10 region of the tile map but on larger scales the procedural generation of the collision boxes becomes rather expensive and i cant see any way to improve the efficiency of the system by a large enough margin to make it viable.

    so now as a placeholder i'm using simple physics script that handles basic input, gravity and collision with the tilemap. it calculates the collision by predicting what the next location of the object will be on both the x and y axis, the next location is then altered depending if the change on either axis results in an overlap with the tile map. very simple stuff.

    i'm now looking at the possibility of creating a script that will act like one of unity's built-in colliders so to allow me to use the built-in physics engine while avoiding the cost of generating ploygon colliders.

    i would appreciate it if anyone has any advice or experience with this subject. i would also like to know how exactly do the colliders and rigidbodys communicate with each other. do they just use the message system, e.g. OnCollisionEnter.
     
  2. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
    As far as I know, yes, the colliders and rigidbodies communicate by the message system such as OnCollisionEnter.

    Im not exactly sure as to what you need, but a custom collider is basically a Mesh Collider http://docs.unity3d.com/Manual/class-MeshCollider.html

    After your mesh is made, you can add the mesh collider and it will be exactly what your mesh is.
    Any changes you do to the mesh, you will need to either remove and readd the mesh collider to update it, or you might be able to set the mesh of the mesh collider to null and then reassign the sharedmesh.
     
  3. massey_digital

    massey_digital

    Joined:
    Apr 28, 2013
    Posts:
    94
    I'm not sure if this completely answers your question, but using mesh(custom) coliders can be pretty intensive if there are many polygons. It would be optimal to use the least number of polygons that achieve the desired effect for the collider (i.e. cube collider has 6 faces can be used for a barrel instead of custom cyndrilical collider).

    Here is more info on colliders and rigidbodies from the Unity Manual:
    http://docs.unity3d.com/Manual/PhysicsOverview.html