Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Move 2D worm with colliders

Discussion in '2D' started by GelozGames, May 26, 2023.

  1. GelozGames

    GelozGames

    Joined:
    Oct 28, 2021
    Posts:
    4
    I have a worm (as in terraria) consisting of a head following by several body parts. The head and each part have a collider. The parent of all this objects have a script to detect collisions and a 2D rigidbody.

    My question is : how am I supposed to move and rotate the worm ?
    Do I have to move each part with Transform or is there something else in this case ?
     
  2. DavidTeo

    DavidTeo

    Unity Technologies

    Joined:
    Apr 1, 2021
    Posts:
    68
  3. GelozGames

    GelozGames

    Joined:
    Oct 28, 2021
    Posts:
    4
  4. AngryProgrammer

    AngryProgrammer

    Joined:
    Jun 4, 2019
    Posts:
    431
    Want to recreate the game like Snake?
     
  5. GelozGames

    GelozGames

    Joined:
    Oct 28, 2021
    Posts:
    4
    I want to recreate the Terarria worm
     
  6. AngryProgrammer

    AngryProgrammer

    Joined:
    Jun 4, 2019
    Posts:
    431
    Any worm/snake-style movement is similar. You have a few GameObjects that create a body of the creature. Each body part must know where to go (a list sequence structure). The head is the start of a list, the second element will have a head reference, and the third element will have the second part reference, etc. All end at the tail (end of the list).

    When we have structure logic, we create one script that tracks the object ahead of him (by the reference you already have).
     
  7. GelozGames

    GelozGames

    Joined:
    Oct 28, 2021
    Posts:
    4
    That's what I did. But how do you move this type of object?
    I mean, all parts have a collider and I want the parent to have the rigidBody. Theoretically, we should move objects with colliders thanks to their rigidBody. But how do we do this when the colliders are in the children of the rigidBody object?
     
  8. AngryProgrammer

    AngryProgrammer

    Joined:
    Jun 4, 2019
    Posts:
    431
    In the primitive form, you can do it by yourself without Rigidbodies. But if you want Rigidbody maybe HingeJoints2D will be a better direction for body, connected to head object you will controll. Later you add some limit how much it can turn so you don't turn full 180 degree.

    Edit: Like dragging a pendant on a table.