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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Question 2D Physics based rope swinging

Discussion in '2D' started by CardboardKnight, Mar 14, 2021.

  1. CardboardKnight

    CardboardKnight

    Joined:
    Mar 8, 2021
    Posts:
    10
    Hi!

    Im somewhat new to game making and i wanted to challenge myself by making a somewhat simple game in a week.
    The game is a 2d platformer with physics based rope swinging as a core feature.
    To illustrate what im working with right now here's a simple representation.
    example.PNG
    The player is the yellow cube, attached to it is a children gameobject acting as his 'Arm' that constantly points towards the mouse. I have a rope next to it made of several segments that moves and curves thanks to the game engine. I would like to make it so that when the player grabs onto the rope the arm locks onto the correct rope segment, stops pointing towards the mouse and simply starts swinging with the rope. I got most of the work done, the arm locks onto the rope, and when there is no "player" and simply the arm, it swings around and rotates on the rope as i want it to.

    However, when the arm is a child of the 'player' and it grabs onto the rope, the arm and player "freeze" in midair, no physics seem to be applied to them even though they are both dynamic objects. I am disabling the movement script of the player while he is grabbed onto a rope for testing purposes, yet he simply will not follow the arm, instead either freezing in the air or plummeting to the ground and leaving the arm floating.

    I have pinpointed the issue to come from the "parent-child" relation of the two objects, since if i create an arm, give it a "player" child gameobject it swings just fine, the arm following the rope and the 'player' following the arm. I dont think that swapping around the "parent child" relation of the arm and player while swinging is a good idea, but i do not quite know how to solve the issue.

    Any help as to how i could solve this issue would be greatly valued. If i can somehow add a bit of weight to the player so that he doesnt feel like a feather, that would also be a wonderful addition. Thanks in advance.
     
  2. CardboardKnight

    CardboardKnight

    Joined:
    Mar 8, 2021
    Posts:
    10
    It seems i have made some progress.

    The first thing i did was to remove the parent child link between the body and the arm, then i added a hinge joint 2d constraint between the two so that the arm would rotate within a "socket" nestled within the player. After some modifications to the player controller and the arm properties i am able to make the player swing on the ropes! There are some issues born from the new relationship between the new objects but they seem overall fairly minor.