Select Git revision
FPSController.cs
-
David Huss authoredDavid Huss authored
FPSController.cs 9.27 KiB
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// Generic First-Person-Shooter-like Controller
[RequireComponent(typeof(CharacterController))]
public class FPSController : MonoBehaviour
{
[Tooltip("How fast the player moves when walking (default move speed).")]
[SerializeField]
private float movementWalkSpeed = 6.0f;
[Tooltip("How fast the player moves when running.")]
[SerializeField]
private float movementRunSpeed = 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 movementLimitDiagonalSpeed = true;
[Tooltip("If checked, the run key toggles between running and walking. Otherwise player runs if the key is held down.")]
[SerializeField]
private bool movementToggleRun = false;
[Tooltip("How high the player jumps when hitting the jump button.")]
[SerializeField]
private float movementJumpSpeed = 8.0f;
[Tooltip("How fast the player falls when not standing on anything.")]
[SerializeField]
private float movementGravity = 20.0f;
[Tooltip("Units that player can fall before a falling function is run. To disable, type \"infinity\" in the inspector.")]
[SerializeField]
private float movementFallingThreshold = 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 movementSlideWhenOverSlopeLimit = 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 movementSlideOnTaggedObjects = false;
[Tooltip("How fast the player slides when on slopes as defined above.")]
[SerializeField]
private float movementSlideSpeed = 12.0f;
[Tooltip("If checked, then the player can change direction while in the air.")]
[SerializeField]
private bool movementAirControl = false;
[Tooltip("Small amounts of this results in bumping when walking down slopes, but large amounts results in falling too fast.")]
[SerializeField]
private float movementAntiBumpFactor = .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 movementAntiBunnyHopFactor = 1;
private Vector3 movementMoveDirection = Vector3.zero;
private bool movementGrounded = false;
private CharacterController movementController;
private Transform movementTransform;
private float movementSpeed;
private RaycastHit movementHit;
private float movementFallStartLevel;
private bool movementFalling;
private float movementSlideLimit;
private float movementRayDistance;