The bug that makes the game

I fixed a lot of bugs rewriting Grapple's code but there's one that I had to add back because Grapple is unplayable without it. In fact without this bug I don't think grapple would ever have been half as fun as it is.

In the original physics code this is how I handled adjusting the player's velocity when swinging on a rope:

...earlier code...
//adjust the direction until it points to inside the range of the grapple
int breakLoop = 0;
while (Vector3.Distance(vertlet.transform.position, transform.position + direction) > vertlet.length)
direction = Vector3.Slerp(direction, vectorTowardsVert, momentumAdjustment * (Time.deltaTime * 50));
if (breakLoop > 500)
...more code...
This is a small chunk of the swinging code which was all one function with more code before and after this.

I didn't spot this, I'll excuse myself by saying that I wasn't looking for a bug in it because it was working fine. The bug is the 10th line, return; I think my thinking was that it should only adjust the player's direction by so much each frame, if it adjusts 500 times an still doesn't meet the requirements just leave it. However instead when the adjustment is too big it reaches 500 and returns from the entire function never running the code after it. I guess it shows the importance of stepping through your code.

This is what the Grapple map "wrap around" is supposed to play like and how it works with the original code:

In my rewrite I used break instead of return and got this instead:

When the momentum adjustment is too big to handle in one frame the function returns early and doesn't run the code that comes after and applies drag. The adjustment is largest when the player is moving fast and the rope length is short so you stop loosing momentum when you're swinging fast on a short rope but the rest of the time it works normally. This makes it possible to pull off really cool swings. Now looking at both gifs I'd say the second one is more realistic but playing the first one doesn't feel too unrealistic and it's way more fun! So completely by chance I have a feature that lets you do crazy stuff when you're swinging really fast on a short rope. I think it's fantastic!

Grapple would not be any fun if it followed the the law of gravity to the letter and I don't know if I ever would have tried an effect like this intentionally so it's very lucky. However it seems that it's effecting game play based on your frame rate, it gets harder to keep your speed the higher your frame rate so I can't just label it a feature right away but I will as soon as I can work out the kinks.

4th Dec, 2014