FPSWalkerEnhanced

From Unify Community Wiki
Jump to: navigation, search

Contents

Description

This is an enhanced C# port of the original FPSWalker script found in the Unity 2.x Standard Assets. It adds walking and running, sliding down slopes (both those above the Slope Limit and/or specific tagged objects), air control, anti-bunny hopping, the ability to check fall distances, and special 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.

Original author: Eric Haines (Eric5h5)

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.

Slide When Over Slope Limit

If checked, the player will slide down slopes that are greater than the Slope Limit as defined by the CharacterController. 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.

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.

Falling Threshold

How many units the player can fall before triggering the OnFell function. The script as-is merely prints the total number of units that the player fell if this happens but the function can be changed to do anything the programmer desires. The "fallDistance" variable in this function contains the number of vertical units between the initial point of "catching air" and the final impact point. If falling function is irrelevant simply change the number to "Infinity" in the inspector, and the function will never be called.

FPSWalkerEnhanced.cs

using UnityEngine;
 
[RequireComponent(typeof(CharacterController))]
public class FPSWalkerEnhanced : MonoBehaviour
{
    [Tooltip("How fast the player moves when walking (default move speed).")]
    [SerializeField]
    private float m_WalkSpeed = 6.0f;
 
    [Tooltip("How fast the player moves when running.")]
    [SerializeField]
    private float m_RunSpeed = 11.0f;
 
    [Tooltip("If true, diagonal speed (when strafing + moving forward or back) can't exceed normal move speed; otherwise it's about 1.4 times faster.")]
    [SerializeField]
    public bool m_LimitDiagonalSpeed = true;
 
    [Tooltip("If checked, the run key toggles between running and walking. Otherwise player runs if the key is held down.")]
    [SerializeField]
    private bool m_ToggleRun = false;
 
    [Tooltip("How high the player jumps when hitting the jump button.")]
    [SerializeField]
    private float m_JumpSpeed = 8.0f;
 
    [Tooltip("How fast the player falls when not standing on anything.")]
    [SerializeField]
    private float m_Gravity = 20.0f;
 
    [Tooltip("Units that player can fall before a falling function is run. To disable, type \"infinity\" in the inspector.")]
    [SerializeField]
    private float m_FallingThreshold = 10.0f;
 
    [Tooltip("If the player ends up on a slope which is at least the Slope Limit as set on the character controller, then he will slide down.")]
    [SerializeField]
    private bool m_SlideWhenOverSlopeLimit = false;
 
    [Tooltip("If checked and the player is on an object tagged \"Slide\", he will slide down it regardless of the slope limit.")]
    [SerializeField]
    private bool m_SlideOnTaggedObjects = false;
 
    [Tooltip("How fast the player slides when on slopes as defined above.")]
    [SerializeField]
    private float m_SlideSpeed = 12.0f;
 
    [Tooltip("If checked, then the player can change direction while in the air.")]
    [SerializeField]
    private bool m_AirControl = false;
 
    [Tooltip("Small amounts of this results in bumping when walking down slopes, but large amounts results in falling too fast.")]
    [SerializeField]
    private float m_AntiBumpFactor = .75f;
 
    [Tooltip("Player must be grounded for at least this many physics frames before being able to jump again; set to 0 to allow bunny hopping.")]
    [SerializeField]
    private int m_AntiBunnyHopFactor = 1;
 
    private Vector3 m_MoveDirection = Vector3.zero;
    private bool m_Grounded = false;
    private CharacterController m_Controller;
    private Transform m_Transform;
    private float m_Speed;
    private RaycastHit m_Hit;
    private float m_FallStartLevel;
    private bool m_Falling;
    private float m_SlideLimit;
    private float m_RayDistance;
    private Vector3 m_ContactPoint;
    private bool m_PlayerControl = false;
    private int m_JumpTimer;
 
 
    private void Start()
    {
        // Saving component references to improve performance.
        m_Transform = GetComponent<Transform>();
        m_Controller = GetComponent<CharacterController>();
 
        // Setting initial values.
        m_Speed = m_WalkSpeed;
        m_RayDistance = m_Controller.height * .5f + m_Controller.radius;
        m_SlideLimit = m_Controller.slopeLimit - .1f;
        m_JumpTimer = m_AntiBunnyHopFactor;
    }
 
 
    private void Update()
    {
        // If the run button is set to toggle, then switch between walk/run speed. (We use Update for this...
        // FixedUpdate is a poor place to use GetButtonDown, since it doesn't necessarily run every frame and can miss the event)
        if (m_ToggleRun && m_Grounded && Input.GetButtonDown("Run"))
        {
            m_Speed = (m_Speed == m_WalkSpeed ? m_RunSpeed : m_WalkSpeed);
        }
    }
 
 
    private void FixedUpdate()
    {
        float inputX = Input.GetAxis("Horizontal");
        float inputY = Input.GetAxis("Vertical");
 
        // If both horizontal and vertical are used simultaneously, limit speed (if allowed), so the total doesn't exceed normal move speed
        float inputModifyFactor = (inputX != 0.0f && inputY != 0.0f && m_LimitDiagonalSpeed) ? .7071f : 1.0f;
 
        if (m_Grounded)
        {
            bool sliding = false;
            // See if surface immediately below should be slid down. We use this normally rather than a ControllerColliderHit point,
            // because that interferes with step climbing amongst other annoyances
            if (Physics.Raycast(m_Transform.position, -Vector3.up, out m_Hit, m_RayDistance))
            {
                if (Vector3.Angle(m_Hit.normal, Vector3.up) > m_SlideLimit)
                {
                    sliding = true;
                }
            }
            // However, just raycasting straight down from the center can fail when on steep slopes
            // So if the above raycast didn't catch anything, raycast down from the stored ControllerColliderHit point instead
            else
            {
                Physics.Raycast(m_ContactPoint + Vector3.up, -Vector3.up, out m_Hit);
                if (Vector3.Angle(m_Hit.normal, Vector3.up) > m_SlideLimit)
                {
                    sliding = true;
                }
            }
 
            // If we were falling, and we fell a vertical distance greater than the threshold, run a falling damage routine
            if (m_Falling)
            {
                m_Falling = false;
                if (m_Transform.position.y < m_FallStartLevel - m_FallingThreshold)
                {
                    OnFell(m_FallStartLevel - m_Transform.position.y);
                }
            }
 
            // If running isn't on a toggle, then use the appropriate speed depending on whether the run button is down
            if (!m_ToggleRun)
            {
                m_Speed = Input.GetKey(KeyCode.LeftShift) ? m_RunSpeed : m_WalkSpeed;
            }
 
            // If sliding (and it's allowed), or if we're on an object tagged "Slide", get a vector pointing down the slope we're on
            if ((sliding && m_SlideWhenOverSlopeLimit) || (m_SlideOnTaggedObjects && m_Hit.collider.tag == "Slide"))
            {
                Vector3 hitNormal = m_Hit.normal;
                m_MoveDirection = new Vector3(hitNormal.x, -hitNormal.y, hitNormal.z);
                Vector3.OrthoNormalize(ref hitNormal, ref m_MoveDirection);
                m_MoveDirection *= m_SlideSpeed;
                m_PlayerControl = false;
            }
            // Otherwise recalculate moveDirection directly from axes, adding a bit of -y to avoid bumping down inclines
            else
            {
                m_MoveDirection = new Vector3(inputX * inputModifyFactor, -m_AntiBumpFactor, inputY * inputModifyFactor);
                m_MoveDirection = m_Transform.TransformDirection(m_MoveDirection) * m_Speed;
                m_PlayerControl = true;
            }
 
            // Jump! But only if the jump button has been released and player has been grounded for a given number of frames
            if (!Input.GetButton("Jump"))
            {
                m_JumpTimer++;
            }
            else if (m_JumpTimer >= m_AntiBunnyHopFactor)
            {
                m_MoveDirection.y = m_JumpSpeed;
                m_JumpTimer = 0;
            }
        }
        else
        {
            // If we stepped over a cliff or something, set the height at which we started falling
            if (!m_Falling)
            {
                m_Falling = true;
                m_FallStartLevel = m_Transform.position.y;
            }
 
            // If air control is allowed, check movement but don't touch the y component
            if (m_AirControl && m_PlayerControl)
            {
                m_MoveDirection.x = inputX * m_Speed * inputModifyFactor;
                m_MoveDirection.z = inputY * m_Speed * inputModifyFactor;
                m_MoveDirection = m_Transform.TransformDirection(m_MoveDirection);
            }
        }
 
        // Apply gravity
        m_MoveDirection.y -= m_Gravity * Time.deltaTime;
 
        // Move the controller, and set grounded true or false depending on whether we're standing on something
        m_Grounded = (m_Controller.Move(m_MoveDirection * Time.deltaTime) & CollisionFlags.Below) != 0;
    }
 
 
    // Store point that we're in contact with for use in FixedUpdate if needed
    private void OnControllerColliderHit(ControllerColliderHit hit)
    {
        m_ContactPoint = hit.point;
    }
 
 
    // This is the place to apply things like fall damage. You can give the player hitpoints and remove some
    // of them based on the distance fallen, play sound effects, etc.
    private void OnFell(float fallDistance)
    {
        print("Ouch! Fell " + fallDistance + " units!");
    }
}
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox