FPSWalkerEnhanced

Author: Eric Haines (Eric5h5)

Description
This is an enhanced version of the FPSWalker script found in Standard Assets. It allows for both walking and running (either by toggle or by holding down a run key), the capability of detecting falling distances, optionally sliding down slopes (both those above the Slope Limit and/or objects specifically tagged "Slide"), optional air control, optional anti-bunny hopping control, and special patented* Anti-Bump™ code that eliminates that irritating "bumpity bumpity bumpa bump" you get when trying to walk down moderately inclined slopes when using the default script.

*It's not really patented...it's just a small change actually.

Note that the new first-person controller prefab in Unity 3 handles most of this, as well as having other functionality.

Usage
In any situation where you'd normally use the standard FPSWalker script, you can replace it with this instead. The original parameters are still there and work as usual (although "speed" is now "walk speed").


 * Walk Speed: How fast the player moves when walking (the default).
 * Run Speed: How fast the player moves when running.
 * Limit Diagonal Speed: If checked, strafing combined with moving forward or backward can't exceed the normal movement rate. The horizontal and vertical movement axes are computed independently, so if this isn't checked, then diagonal speed is about 1.4 times faster than normal.
 * Toggle Run: If checked, the player can toggle between running and walking by pressing the run button (there must be a button set up in the Input Manager called "Run"). If not checked, the player normally walks unless holding down the run button.
 * Jump Speed: How high the player jumps when hitting the jump button (there must be a button set up in the Input Manager called "Jump", which there is by default).
 * Gravity: How fast the player falls when not standing on anything.
 * Falling Damage Threshold: How many units the player can fall before taking damage when landing. The script as-is merely prints the total number of units that the player fell if this happens, but the FallingDamageAlert function can be changed to do anything the programmer desires. The "fallDistance" local variable in this function contains the number of vertical units between the initial point of "catching air" and the final impact point. If falling damage is irrelevant, change the number to "Infinity" in the inspector, and the function will never be called.
 * Slide When Over Slope Limit: If checked, the player will slide down slopes that are greater than the Slope Limit as defined by the Character Controller. Attempting to jump up such slopes will also fail. The player has no control until resting on a surface that is under the Slope Limit.
 * Slide On Tagged Objects: If checked, the player will slide down any objects tagged "Slide" when standing on them, regardless of how much or little they are sloped. (The tag "Slide" must be added to the Tag Manager.) This can be used to create chutes, icy surfaces, etc. Note that tagged objects with zero slope will cause sliding in an undefined direction.
 * Slide Speed: How fast the player slides when on slopes as defined above.
 * Air Control: If checked, the player will be able to control movement while in the air, except when Slide When Over Slope Limit/Slide On Tagged Objects is enabled and the player is jumping off a slope over the limit (otherwise the player would be able to jump up supposedly inaccessible slopes).
 * Anti Bump Factor: An amount used to reduce or eliminate the "bumping" that can occur when walking down slopes, which is a result of the player constantly toggling between walking and falling small distances. The default of .75 is sufficient for most cases, although a little bit can still occur on steep slopes, assuming sliding isn't enabled. Larger amounts will stop this from ever happening, but too much can result in excessive falling speeds when stepping over an edge. Very small amounts will enable bumping, if that's desired for some reason.
 * Anti Bunny Hop Factor: Bunny hopping is repeated jumping by virtue of continuously holding down the jump button. Often considered annoying and silly, especially in multiplayer games. If the anti-hop value is at least 1, the player must release the jump button (and the release-and-hold-in-the-air trick is ineffective), and be grounded for the specified number of physics frames before being able to jump again. If the value is 0, then bunny hopping is allowed. If using noticeably large values, implementing some kind of visual indicator of jump availability is recommended to avoid player frustration.

The slope sliding is quite basic: the player is either sliding or not, and has no lateral control when sliding. One issue that may surface is that, under some circumstances, attempting to force oneself up a slippery slope will result in annoying jittering, as the character controller moves forward a bit one frame only to slide back the next frame, then moves forward a bit again, etc. Possible solutions are: add code to the script so this doesn't happen, design levels so as to minimize this, or ignore it and repeat to yourself "If it's good enough for UT2004, it's good enough for me." It's also not a good idea to create situations where the player slides into a V-shaped wedge valley where both slopes are unscalable, since the player will be stuck at the bottom, jittering for eternity. Not fun.

C# Version
Identical script, just converted to C#.

Converted by: --TwiiK 09:40, 1 May 2010 (PDT)