# VariableSpeedFPSwalker

From Unify Community Wiki

Author: (Mr. Animator)

## Description

- 1: Script allows you to have different speeds for walking forward, strafing, and backpeddling (I hate shooters that let you RUN backwards)
- 2: Because it uses rigidbody.velocity instead of rigidbody.AddRelativeForce, it's more directly tied to the settings you choose for the Horizontal/Vertical axes, and your speed plateaus nice and dependably. I found the default FPS walker to be a little too loosey goosey
- 3: To keep you from magically gaining extra speed for walking forward and strafing at the same time (a little side-effect from my method of doing things) there's a semi-sloppy math solution in there that "rounds off" the corners. So if you're moving on a 45 degree angle, it properly adjusts your forward and lateral movement so that they don't add up to more than you would get for just walking forward.

## Usage

Assign this script to any RigidBody you want to be user controled.

## JavaScript - VariableSpeedFPSwalker.js

var forwardSpeed = 10; var backwardSpeed = 5; var strafeSpeed = 8; function FixedUpdate () { // Step1: Get your input values horizontal = Input.GetAxis("Horizontal"); vertical = Input.GetAxis("Vertical"); // Step 2: Limit the movement on angles and then multiply those results // by their appropriate speed variables percentofpercent = Mathf.Abs(horizontal) + Mathf.Abs(vertical) - 1.0; if (percentofpercent > 0.1) { // if we're here, then we're not moving in a straight line // my math here might be kinda confusing and sloppy...so don't look! percentofpercent = percentofpercent * 10000; percentofpercent = Mathf.Sqrt(percentofpercent); percentofpercent = percentofpercent / 100; finalMultiplier = percentofpercent * .25; horizontal = horizontal - (horizontal * finalMultiplier); vertical = vertical - (vertical * finalMultiplier); } if (vertical > 0) { vertical = vertical * forwardSpeed; } if (vertical < 0) { vertical = vertical * backwardSpeed; } horizontal = horizontal * strafeSpeed; // Step 3: Derive a vector on which to travel, based on the combined // influence of BOTH axes tubeFinalVector = transform.TransformDirection (Vector3(horizontal,0,vertical)); // Step 4: Apply the final movement in world space rigidbody.velocity.z = tubeFinalVector.z; rigidbody.velocity.x = tubeFinalVector.x; } function Awake () { rigidbody.freezeRotation = true; }