In Grapple Gunner the only thing you have is a grappling hook, so the rope has to feel right. The obvious version, a straight line from the gun to the hook, falls apart as soon as you swing past a wall: the line cuts straight through it and you keep swinging around a point you can’t see. This is how I made the rope wrap around corners instead, in a game jam, with a handful of raycasts.
The rope is a list of points
The rope isn’t a physics chain. It’s a list of positions, drawn with a line renderer:
Points[0] Points[1] Points[2]
gun ●───────────● corner ─────────◆ hookPoints[0]is the gun, and is updated every tick as you move.- The last point is the hook.
- Everything in between is a corner the rope is currently bent around.
Wrapping means inserting a corner into the list. Unwrapping means removing one. That’s the whole trick; the rest is deciding when.
Corners are marked in the level
Every wall has four tiny box colliders, one on each corner, on their own Node layer. They’re the only places the rope can bend. That keeps the search tiny: the rope never has to work out where the corner of a wall is, it just has to notice when it touches one.
■────────────■
│ │ ■ = corner node (Node layer)
│ wall │
│ │
■────────────■Wrapping: a ray from each end
Every physics tick, each end of the rope fires a ray towards its neighbouring point. If something is in the way and it’s a corner node, the rope bends there.
Before: the rope cuts through the wall After: the corner is inserted
◆ hook ◆ hook
\ │
■─\──────────■ ■────────────■
│ \ wall │ │ wall │
│ \ │ │ │
■────\───────■ ●────────────■
\ \ Points[1] = this corner
● gun ● gunOn the gun’s side the corner is inserted at index 1, right after the gun. On the hook’s side it goes just before the hook. Both check the corner isn’t already the neighbouring point, so the same corner isn’t added twice.
Unwrapping: can you see past the bend?
Wrapping on its own would leave the rope stuck on every corner it ever touched. So each end also checks whether it can see the point beyond its nearest bend. If nothing is in the way, the bend isn’t holding the rope any more and is removed.
You swing back left: the gun can see the hook again, so the corner goes
◆ hook
╱│
╱ ■────────────■
╱ │ wall │
● │ │
gun ●────────────■
└ Points[1] is no longer needed: removedThe whole loop
Simplified, every physics tick does this:
// Points[0] = gun, Points[^1] = hook, everything between = corners the rope is bent round
void FixedUpdate()
{
Points[0] = gun.position;
Points[^1] = hook.position;
// Wrap: is a corner between an end and its neighbour? Bend there.
if (RayHitsCorner(gun, Points[1], out var c) && c != Points[1]) Points.Insert(1, c);
if (RayHitsCorner(hook, Points[^2], out var d) && d != Points[^2]) Points.Insert(Points.Count - 1, d);
// Unwrap: can an end see past its nearest bend? Then the bend isn't needed.
if (Points.Count > 2 && CanSee(gun, Points[2])) Points.RemoveAt(1);
if (Points.Count > 2 && CanSee(hook, Points[^3])) Points.RemoveAt(Points.Count - 2);
line.positionCount = Points.Count;
line.SetPositions(Points.ToArray());
}Why it matters for the swing
The list isn’t just for drawing. When you reel in, the player is pulled towards Points[1], the first point on the rope, not the hook:
◆ hook
│
■────────────■
│ wall │
●────────────■
↖ pull goes to Points[1], the corner
● gunSo when the rope wraps, your pivot moves to the corner, exactly like a real rope. Hook a crate instead of a wall and the pull runs the other way: the crate is pulled towards the second-to-last point, so it comes round the corners towards you.
What went wrong in the jam version
The jam code worked well enough to ship, but it had problems I fixed when I polished the game in 2026:
- The rays had no length limit. The ray length was baked into the direction vector, but Unity’s
Physics2D.Raycasttakes the distance as a separate argument, so every ray went on forever. - A ray that hit nothing crashed. The code read
hit.transformwithout checking there was a hit, which threw null-reference errors whenever a ray missed. - It logged to the console every frame while you reeled in.
The 2026 version also gives the rope a maximum length, so it can go slack.
What I’d change
- Find corners from the geometry. Hand-placed corner nodes mean the rope only wraps where someone remembered to add them.
- Unwrap by angle. Storing which way the rope bent around each corner and removing the bend when that flips is cheaper than re-testing line of sight, and doesn’t depend on a ray hitting a tiny collider.