Change Log
Change Log
//
//
// "Enable/Disable Headbob, Changed look rotations - should result in reduced camera jitters" ||
version 1.0.1
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
#if UNITY_EDITOR
using UnityEditor;
using System.Net;
#endif
// Crosshair
// Internal Variables
// Internal Variables
#endregion
#endregion
// Internal Variables
#region Sprint
// Sprint Bar
// Internal Variables
#endregion
#region Jump
// Internal Variables
#endregion
#region Crouch
// Internal Variables
#endregion
#endregion
#region Head Bob
// Internal Variables
#endregion
rb = GetComponent<Rigidbody>();
crosshairObject = GetComponentInChildren<Image>();
playerCamera.fieldOfView = fov;
originalScale = transform.localScale;
jointOriginalPos = joint.localPosition;
if (!unlimitedSprint)
sprintRemaining = sprintDuration;
sprintCooldownReset = sprintCooldown;
}
void Start()
material = GetComponent<Renderer>().material;
characterController = GetComponent<CharacterController>();
if (lockCursor)
Cursor.lockState = CursorLockMode.Locked;
if(crosshair)
crosshairObject.sprite = crosshairImage;
crosshairObject.color = crosshairColor;
else
crosshairObject.gameObject.SetActive(false);
sprintBarCG = GetComponentInChildren<CanvasGroup>();
if(useSprintBar)
sprintBarBG.gameObject.SetActive(true);
sprintBar.gameObject.SetActive(true);
if(hideBarWhenFull)
sprintBarCG.alpha = 0;
else
sprintBarBG.gameObject.SetActive(false);
sprintBar.gameObject.SetActive(false);
#endregion
float camRotation;
#region Camera
if(cameraCanMove)
else
// Inverted Y
if (enableZoom)
if (!isZoomed)
isZoomed = true;
else
{
isZoomed = false;
if(Input.GetKeyDown(zoomKey))
isZoomed = true;
else if(Input.GetKeyUp(zoomKey))
isZoomed = false;
if(isZoomed)
}
#endregion
#endregion
#region Sprint
if(enableSprint)
if(isSprinting)
isZoomed = false;
if(!unlimitedSprint)
sprintRemaining -= 1 * Time.deltaTime;
if (sprintRemaining <= 0)
isSprinting = false;
isSprintCooldown = true;
else
if(isSprintCooldown)
sprintCooldown -= 1 * Time.deltaTime;
if (sprintCooldown <= 0)
isSprintCooldown = false;
else
sprintCooldown = sprintCooldownReset;
// Handles sprintBar
#endregion
#region Jump
Jump();
}
#endregion
#region Crouch
if (enableCrouch)
Crouch();
isCrouched = false;
Crouch();
isCrouched = true;
Crouch();
#endregion
CheckGround();
if(enableHeadBob)
HeadBob();
}
if (IsSliding())
distance = slideDistance;
characterController.Move(slideDirection * distance);
StopSliding();
if (material == null)
Debug.LogError("Material is null");
return;
void FixedUpdate()
#region Movement
if (playerCanMove)
isWalking = true;
else
isWalking = false;
velocityChange.y = 0;
if (velocityChange.x != 0 || velocityChange.z != 0)
isSprinting = true;
if (isCrouched)
Crouch();
sprintBarCG.alpha += 5 * Time.deltaTime;
rb.AddForce(velocityChange, ForceMode.VelocityChange);
else
isSprinting = false;
sprintBarCG.alpha -= 3 * Time.deltaTime;
velocityChange.y = 0;
rb.AddForce(velocityChange, ForceMode.VelocityChange);
#endregion
// Sets isGrounded based on a raycast sent straigth down from the player object
isGrounded = true;
else
isGrounded = false;
}
}
if (isGrounded)
isGrounded = false;
// When crouched and using toggle system, will uncrouch for a jump
Crouch();
if(isCrouched)
walkSpeed /= speedReduction;
isCrouched = false;
// Reduces walkSpeed
else
walkSpeed *= speedReduction;
isCrouched = true;
if(isWalking)
if(isSprinting)
else if (isCrouched)
else
else
timer = 0;
slideDirection = Vector3.zero;
slideStartTime = 0f;
{
slideDirection = direction;
slideStartTime = Time.time;
// Custom Editor
#if UNITY_EDITOR
[CustomEditor(typeof(FirstPersonController)), InitializeOnLoadAttribute]
FirstPersonController fpc;
SerializedObject SerFPC;
fpc = (FirstPersonController)target;
SerFPC.Update();
EditorGUILayout.Space();
EditorGUILayout.LabelField("", GUI.skin.horizontalSlider);
EditorGUILayout.Space();
GUI.enabled = fpc.cameraCanMove;
GUI.enabled = true;
if(fpc.crosshair)
{
EditorGUI.indentLevel++;
EditorGUILayout.BeginHorizontal();
EditorGUILayout.EndHorizontal();
EditorGUILayout.BeginHorizontal();
EditorGUILayout.EndHorizontal();
EditorGUI.indentLevel--;
EditorGUILayout.Space();
GUI.enabled = fpc.enableZoom;
#endregion
#endregion
EditorGUILayout.LabelField("", GUI.skin.horizontalSlider);
EditorGUILayout.Space();
GUI.enabled = fpc.playerCanMove;
GUI.enabled = true;
EditorGUILayout.Space();
#region Sprint
GUI.enabled = fpc.enableSprint;
fpc.unlimitedSprint = EditorGUILayout.ToggleLeft(new GUIContent("Unlimited Sprint",
"Determines if 'Sprint Duration' is enabled. Turning this on will allow for unlimited sprint."),
fpc.unlimitedSprint);
//GUI.enabled = !fpc.unlimitedSprint;
//GUI.enabled = true;
if(fpc.useSprintBar)
EditorGUI.indentLevel++;
EditorGUILayout.BeginHorizontal();
EditorGUILayout.EndHorizontal();
EditorGUILayout.BeginHorizontal();
EditorGUILayout.EndHorizontal();
EditorGUILayout.BeginHorizontal();
EditorGUILayout.EndHorizontal();
EditorGUILayout.BeginHorizontal();
EditorGUILayout.EndHorizontal();
EditorGUILayout.BeginHorizontal();
EditorGUILayout.EndHorizontal();
EditorGUI.indentLevel--;
GUI.enabled = true;
EditorGUILayout.Space();
#endregion
#region Jump
GUILayout.Label("Jump", new GUIStyle(GUI.skin.label) { alignment = TextAnchor.MiddleLeft,
fontStyle = FontStyle.Bold, fontSize = 13 }, GUILayout.ExpandWidth(true));
GUI.enabled = fpc.enableJump;
GUI.enabled = true;
EditorGUILayout.Space();
#endregion
#region Crouch
GUI.enabled = fpc.enableCrouch;
#endregion
#endregion
EditorGUILayout.Space();
EditorGUILayout.LabelField("", GUI.skin.horizontalSlider);
EditorGUILayout.Space();
GUI.enabled = fpc.enableHeadBob;
GUI.enabled = true;
#endregion
if(GUI.changed)
EditorUtility.SetDirty(fpc);
Undo.RecordObject(fpc, "FPC Change");
SerFPC.ApplyModifiedProperties();
#endif