- Home /
Creating a movement script to move up and down a chain (or rope) in a 2D game
I have created a chain using Hinge Joints, now I want my 2D character to grab this chain and go up and down. I want my player to be attached to this chain so that if the chain moves, the player also moves on the chain realistically. (I think I should use a kind of joint on my player too, but I'm confused) What is the best approach to do this?
i would use a distance joint from the hand of the player to the end of the rope.
Combine that with childing the character to the closest hand joint, and add or remove Y velocity based on input and distance from end point. Then add a function that receives Input to handle attaching and unattaching the player to the rope. Like if he presses an interact key while standing in chain area, it disables player input from normal movement and childs the player to the chain, then changes animations or whatever and waits for Up/Down input to push the player up or down the chain. With left and right, you can add force to the chain to swing it either direction while the player is on it. If the chain is swinging, you'll want to change it from simply adjusting Y velocity, to ins$$anonymous$$d Velocity in the direction of next Upper chain node, or lower chain node if movement is downwards.
So if I understand correctly, you are suggesting that I use a distance joint on my character, and when he's going to attach to the chain, I get the closest chain part and attach its rigidbody to my character's dist joint, is that right?
Answer by Nivbot · Mar 17, 2019 at 08:08 AM
Just attach your player to the chain (make the chain its parent) and then use localPosition of the player transform to move up and down on the chain.
If there’s an issue because each piece of the chain is a separate object you can set the parent to the next link up or down when the player’s collider or the transform is near it
Thank you, so you mean that I don't need to use joints this way, right?
What I do with ladders is just set the rigid body gravity to 0 when on the ladder and just move up and down, but since ladders don’t move I don’t need to parent anything.
Since you want the player to sway left and right with the rope it would probably be easiest to just parent the player to the part of the rope he is on then he will move with it, while you will still be able to move the player up and down with localPosition.
That’s how I’d do it anyway. Someone might and probably does know a better way but it should work.
Your answer
Follow this Question
Related Questions
What is happening in HingeJoint2D motors? 1 Answer
Set targetjoint2d anchor position at mouse position in 2d 0 Answers
Player freezes upon attaching a "connected body" to a hinge/distance joint 0 Answers
Relative Joint2d: Is there a way limit force to only X or Y axis? 0 Answers
How to give a direction to a hook? 1 Answer