Search code examples
c#unity-game-enginecollision-detectioncollider

Detecting an object colliding with itself - snake or rope example


I have a custom mesh I've created for a game object. I want to do some actions when it touches itself. I tried attaching the collider and the script to the mesh. However the script is called only when colliding with other objects. How can I detect colliding with itself?


Solution

  • In a game physics engine, such as PhysX: there is no meaningful sense in which a collider can collide with itself.

    Note that, in games and 3D engineering, "things" almost always have more than one collider.

    For example, a doggie might be made of, oh, seven or so colliders (you can imagine one for the torso (perhaps a sphere there), four legs (probably capsule or just rectangle), one for the head (spehere again), and if relevant in the game one for the tail (probably just a rectangle - or maybe two small spheres). For a car, you might have say three boxes for the overall shape of the body, and you likely have individual colliders for "where you need to know what hit" ... eg, the bumpers, maybe the doors if you expect it to be bashed there, and so on.

    Now,

    regarding your question, it looks like you have -- for example -- a humanoid, which would have a number of colliders as described in the previous paragraph.

    It could be that, for example, the arm collider can hit the leg collider, and you want to know that.

    It is impossible to help you with your question, unless, you explain what your scene is in general terms...

    --

    If you're making a snake or perhaps rope, do what Gunnar said. The simplest solution is a number of sphere colliders.

    Quite simply say your snake is 10 centimeters thick. Say your snake is 2 meters long. Quite simply have sphere colliders about 9-10 cm in size. Separate them so they are not touching one another but a bit spaced. So for 2m you might have about 15 of them. That's all there is to it.

    Please note that engineering ropes / snakes is far from easy.

    I encourage you to glance at the AssetStore and look at the may excellent rope/snake packages available.

    Realise that it can take man-years of engineering to develop a perfect rope system.

    Understand that an everyday aspect of Unity (or game engineering in general), is that you have to use an existing, often well-known solution, for the problem at hand. Good luck.