r/Unity2D • u/ZeSeagull4626 • 1d ago
Question/Help Help with coding a tether
I am currently working on a 2d game where two players are tied to eachother with a rope, I'd use the build in features like a spring joint, distance joint, etc but the problem is that the current code relies on using kinematic rigidbodies so those won't work. So here i am asking for help of how i would program this, I'd also appriciate places where i can learn more about game code in general because the base google searches sure aint doing it for me :p
Here is code i think might be relevant
In my walk state i have:
private void HandleHorizontalMovement()
{
float targetVelocityX = 0f;
if (Mathf.Abs(Ctx.moveInput.x) >= Ctx.moveStats.moveThreshold)
{
float moveDirection = Mathf.Sign(Ctx.moveInput.x);
targetVelocityX = moveDirection * Ctx.moveStats.maxWalkSpeed;
}
float acceleration = Ctx.moveStats.groundAcceleration;
float deceleration = Ctx.moveStats.groundDeceleration;
Vector2 velocity = Ctx.Velocity;
if (Mathf.Abs(Ctx.moveInput.x) >= Ctx.moveStats.moveThreshold)
{
velocity.x = Mathf.Lerp(velocity.x, targetVelocityX, acceleration * Time.fixedDeltaTime);
}
else
{
velocity.x = Mathf.Lerp(velocity.x, 0, deceleration * Time.fixedDeltaTime);
}
Ctx.Velocity = velocity;
}
Then in the PlayerStateMachine I call this
private void FixedUpdate()
{
CountTimers(Time.fixedDeltaTime);
HandleHeadBumpSlide();
_currentState.UpdateStates();
JumpCheck();
TurnCheck(moveInput);
WallJumpCheck();
ClampVelocity();
Controller.Move(Velocity * Time.fixedDeltaTime);
//reset inputs
jumpWasPressed = false;
jumpWasReleased = false;
dashWasPressed = false;
attackWasPressed = false;
_coyoteJumpEnabled = false;
}
And the movement finnaly gets resolved in a different script
public void Move(Vector2 velocity)
{
bool wasGrounded = isCollidngBelow;
UpdateRaycastCorners();
ResetCollisionState();
CheckCeilingBoxCast(velocity);
float stepOffset = 0f;
ResolveHorizontalMovement(ref velocity, wasGrounded, ref stepOffset);
ResolveVerticalMovement(ref velocity);
rb.MovePosition(rb.position + velocity + Vector2.up * stepOffset);
}
1
u/QueasyPrior6654 1d ago
I'd step both players from one manager script in FixedUpdate instead of letting each run alone. Have both compute their velocity first, then solve the tether before moving.
If their predicted gap passes max length, pull each planned step inward by half the excess and cancel their outward relative velocity for next tick. Split
rb.MovePositionout ofController.Moveso you can run your raycast checks before committing. Recheck the gap after every collision pass using both final targets, including step offsets. If it's too long, pull whichever player can move inward and re-run collisions, only applying moves once collision and length both pass. Do you want a hard length limit, or should the rope stretch a bit?1
u/One_Yam4643 20h ago
For the stretch you asked about: keep the manager idea from the other answer, but instead of snapping both players back to the max length, treat the part past the rest length as a spring and push it into their velocities before Controller.Move runs.
In the manager's FixedUpdate, after both players have worked out their velocity for this tick:
Only stretch is corrected, so a slack rope does nothing, like a real one. The damping term stops it from bouncing back and forth forever. Start with stiffness around 50 and damping around 5 and tune by feel. If your states overwrite velocity from input every tick, keep a separate rope velocity per player that carries over between ticks, and add it right before Move. Add a hard cap (the snap-back from the other answer) at maybe 1.3x the rest length so the rope can't stretch forever when one player is stuck or fast.
One thing in your code to watch: ClampVelocity runs before Move, so if the rope adds velocity and the clamp is based on max walk speed, the clamp will eat the pull. Apply the rope after the clamp, or clamp only the input part.
For learning: Catlike Coding's free Unity tutorials go deep on movement and physics code, and the "Game Programming Patterns" book is free to read online; its Game Loop and Update Method chapters explain exactly this kind of ordering problem.
1
u/Federal-Row2373 5h ago
The physics side is well covered above, so just one thing for the visual side: you don't need a real simulated rope to make it look good. A LineRenderer between the two players works fine, and you can fake the slack by drawing a curve through a midpoint that sags down based on how much shorter the distance is than the rest length (restLength - dist). When the rope is taut the sag goes to zero and it becomes a straight line, which also shows the player at a glance when they're being pulled. Are you planning for one player to hang off a ledge from the other? That case would change how much the spring needs to handle gravity.