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

How to make player climb a physics-driven chain?

Discussion in '2D' started by Jokeman258, Sep 28, 2014.

  1. Jokeman258

    Jokeman258

    Joined:
    Jun 1, 2013
    Posts:
    23
    I have a chain which is made of several 2D rigidbodies stitched together with hinge joints (as shown below). I want to make it so the player can climb this rope up and down, but also stay attached to it if it starts swinging back and forth. Does anyone know the best way to go about doing this?

    Thanks for the help!

    (I wasn't sure if this should go in 2D or scripting, so I just posted in both)

    PlayerAndRope.png
     
  2. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    pos.y = Mathf.Lerp(prevLink.y, nextLink.y, climbPos);

    When climbPos>1 and going up, change prev and next link then set climbPos = 0.
    When climbPos<1 and going down, change prev and next link then set climbPos = 1.

    And for x:

    pos.x = Mathf.Lerp(prevLink.x, nextLink.x, 0.5f);


    Something like that. This way, no matter how violent the motion, the player is always averaged between two segments in the rope, and the player's climbing is controlled by adding or subtracting climbPos. For example: climbPos += GetAxis("Vertical");

    In any case, this is just one approach you can think about and learn from - using tweening or lerping to go between two points in a reliable manner. It's reliable because you're not relying on physics or collisions, but on a single number - climbPos.