Advertisement
Guest User

Untitled

a guest
Apr 1st, 2018
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 52.39 KB | None | 0 0
  1. //FPSPlayer.cs by Azuline Studios© All Rights Reserved
  2. //Controls main player actions such as hitpoints and damage, HUD GUIText/Texture element instantiation and update,
  3. //directs player button mappings to other scripts, handles item detection and pickup, and plays basic player sound effects.
  4. using UnityEngine;
  5. using System.Collections;
  6.  
  7. public class FPSPlayer : MonoBehaviour {
  8.     public Transform Point1;
  9.     public Transform Point2;
  10.     public Transform Monstr;
  11.     public float speedt;
  12.     [HideInInspector]
  13.     public Ironsights IronsightsComponent;
  14.     [HideInInspector]
  15.     public InputControl InputComponent;
  16.     [HideInInspector]
  17.     public FPSRigidBodyWalker FPSWalkerComponent;
  18.     [HideInInspector]
  19.     public PlayerWeapons PlayerWeaponsComponent;
  20.     [HideInInspector]
  21.     public WorldRecenter WorldRecenterComponent;
  22.     [HideInInspector]
  23.     public WeaponBehavior WeaponBehaviorComponent;
  24.     [HideInInspector]
  25.     public  SmoothMouseLook MouseLookComponent;
  26.     [HideInInspector]
  27.     public  WeaponEffects WeaponEffectsComponent;
  28.     [HideInInspector]
  29.     public WeaponPivot WeaponPivotComponent;
  30.     [HideInInspector]
  31.     public CameraControl CameraControlComponent;
  32.     [HideInInspector]
  33.     public NPCRegistry NPCRegistryComponent;
  34.     [HideInInspector]
  35.     public GameObject NPCMgrObj;
  36.     private AI AIComponent;
  37.     //other objects accessed by this script
  38.     [HideInInspector]
  39.     public GameObject[] children;//behaviors of these objects are deactivated when restarting the scene
  40.     [HideInInspector]
  41.     public GameObject weaponCameraObj;
  42.     [HideInInspector]
  43.     public GameObject weaponObj;
  44.     [Tooltip("Object reference to the GUITexture object in the project library that renders pain effects on screen.")]
  45.     public GameObject painFadeObj;
  46.     public GameObject levelLoadFadeObj;
  47.     [Tooltip("Object reference to the GUIText object in the project library that renders health amounts on screen.")]
  48.     public GameObject healthGuiObj;
  49.     [HideInInspector]
  50.     public GameObject healthGuiObjInstance;
  51.     [Tooltip("Object reference to the GUIText object in the project library that renders ammo amounts on screen.")]
  52.     public GameObject ammoGuiObj;
  53.     [Tooltip("Object reference to the GUIText object in the project library that renders hunger amounts on screen.")]
  54.     public GameObject hungerGuiObj;
  55.     [HideInInspector]
  56.     public GameObject hungerGuiObjInstance;
  57.     [Tooltip("Object reference to the GUIText object in the project library that renders thirst amounts on screen.")]
  58.     public GameObject thirstGuiObj;
  59.     [HideInInspector]
  60.     public GameObject thirstGuiObjInstance;
  61.     [Tooltip("Object reference to the GUIText object in the project library that renders help text on screen.")]
  62.     public GameObject helpGuiObj;
  63.     [HideInInspector]
  64.     public GameObject helpGuiObjInstance;
  65.     [Tooltip("Object reference to the GUITexture object in the project library that renders crosshair on screen.")]
  66.     public GameObject CrosshairGuiObj;
  67.     [HideInInspector]
  68.     public GameObject CrosshairGuiObjInstance;
  69.     [HideInInspector]
  70.     public GUITexture CrosshairGuiTexture;
  71.     [Tooltip("Object reference to the GUIText object in the project library that renders hitmarker on screen.")]
  72.     public GameObject hitmarkerGuiObj;
  73.     [HideInInspector]
  74.     public GameObject hitmarkerGuiObjInstance;
  75.     [HideInInspector]
  76.     public GUITexture hitmarkerGuiTexture;
  77.  
  78.     private HealthText HealthText;
  79.     private HealthText[] HealthText2;
  80.     private GUIText healthGuiText;
  81.    
  82.     private HungerText HungerText ;
  83.     private HungerText[] HungerText2;
  84.     private GUIText HungerGUIText;
  85.    
  86.     private ThirstText ThirstText;
  87.     private ThirstText[] ThirstText2;
  88.     private GUIText ThirstGUIText;
  89.        
  90.     [HideInInspector]
  91.     public float crosshairWidth;
  92.     private Rect crossRect;
  93.     [Tooltip("Size of crosshair relative to screen size.")]
  94.     public float crosshairSize;
  95.     private float oldWidth;
  96.     [HideInInspector]
  97.     public float hitTime = -10.0f;
  98.     [HideInInspector]
  99.     public bool hitMarkerState;
  100.     private Transform mainCamTransform;
  101.     [TooltipAttribute("True if the prefab parent object will be removed on scene load.")]
  102.     public bool removePrefabRoot = true;
  103.    
  104.     //player hit points
  105.     public float hitPoints = 100.0f;
  106.     public float maximumHitPoints = 200.0f;
  107.     [Tooltip("True if player's health should be displayed on the screen.")]
  108.     public bool showHealth = true;
  109.     [Tooltip("True if player's ammo should be displayed on the screen.")]
  110.     public bool showAmmo = true;
  111.     [Tooltip("True if negative hitpoint values should be shown.")]
  112.     public bool showHpUnderZero = true;
  113.     [Tooltip("True if player cannot take damage.")]
  114.     public bool invulnerable;
  115.     [Tooltip("True if the player regenerates their health after health regen delay elapses without player taking damage.")]
  116.     public bool regenerateHealth = false;
  117.     [Tooltip("The maximum amount of hitpoints that should be regenerated.")]
  118.     public float maxRegenHealth = 100.0f;
  119.     [Tooltip("Delay after being damaged that the player should start to regenerate health.")]
  120.     public float healthRegenDelay = 7.0f;
  121.     [Tooltip("Rate at which the player should regenerate health.")]
  122.     public float healthRegenRate = 25.0f;
  123.     private float timeLastDamaged;//time that the player was last damaged
  124.    
  125.     //player hunger
  126.     [Tooltip("True if player should have a hunger attribute that increases over time.")]
  127.     public bool usePlayerHunger;
  128.     [HideInInspector]
  129.     public float maxHungerPoints = 100.0f;//maximum amount that hunger will increase to before players starts to starve
  130.     [TooltipAttribute("Seconds it takes for player to accumulate 1 hunger point.")]
  131.     public float hungerInterval = 7.0f;
  132.     [HideInInspector]
  133.     public float hungerPoints = 0.0f;//total hunger points
  134.     private float lastHungerTime;//time that last hunger point was applied
  135.     private float lastStarveTime;//time that last starve damage was applied
  136.     [TooltipAttribute("Seconds to wait before starve damaging again (should be less than healthRegenDelay to prevent healing of starvation damage).")]
  137.     public float starveInterval = 3.0f;
  138.     [TooltipAttribute("Anount of damage to apply per starve interval.")]
  139.     public float starveDmgAmt = -5.0f;//amount to damage player per starve interval
  140.    
  141.     //player thirst
  142.     [Tooltip("True if player should have a thirst attribute that increases over time.")]
  143.     public bool usePlayerThirst;
  144.     [HideInInspector]
  145.     public float maxThirstPoints = 100.0f;//maximum amount that thirst will increase to before players starts to take thirst damage
  146.     [TooltipAttribute("Seconds it takes for player to accumulate 1 thirst point.")]
  147.     public float thirstInterval = 7.0f;
  148.     [HideInInspector]
  149.     public float thirstPoints = 0.0f;//total thirst points
  150.     private float lastThirstTime;//time that last thirst point was applied
  151.     private float lastThirstDmgTime;//time that last thirst damage was applied
  152.     [TooltipAttribute("Seconds to wait before thirst damaging again (should be less than healthRegenDelay to prevent healing of thirst damage).")]
  153.     public float thirstDmgInterval = 3.0f;
  154.     [Tooltip("Amount to damage player per thirst damage interval.")]
  155.     public float thirstDmgAmt = -5.0f;
  156.  
  157.     [Tooltip("True if player can activate bullet time by pressing button (default T).")]
  158.     public bool allowBulletTime = true;
  159.     [Tooltip("True if help text should be displayed.")]
  160.     public bool showHelpText = true;
  161.    
  162.     //Damage feedback
  163.     private float gotHitTimer = -1.0f;
  164.     private Color PainColor = new Color(0.221f, 0f, 0f, 0.44f);//color of pain screen flash can be selected in editor
  165.     public Texture2D painTexture;
  166.     private Color painFadeColor;//used to modify opacity of pain fade object
  167.     [TooltipAttribute("Amount to kick the player's camera view when damaged.")]
  168.     public float painScreenKickAmt = 0.016f;//magnitude of the screen kicks when player takes damage
  169.    
  170.     //Bullet Time and Pausing
  171.     [TooltipAttribute("Percentage of normal time to use when in bullet time.")]
  172.     [Range(0.0f, 1.0f)]
  173.     public float bulletTimeSpeed = 0.35f;//decrease time to this speed when in bullet time
  174.     [Tooltip("Movement multiplier during bullet time.")]
  175.     public float sloMoMoveSpeed = 2.0f;
  176.     private float pausedTime;//time.timescale value to return to after pausing
  177.     [HideInInspector]
  178.     public bool bulletTimeActive;
  179.     [HideInInspector]
  180.     public float backstabBtTime;
  181.     [HideInInspector]
  182.     public bool backstabBtState;
  183.     private float initialFixedTime;
  184.     [HideInInspector]
  185.     public float usePressTime;
  186.     [HideInInspector]
  187.     public float useReleaseTime;
  188.     private bool useState;
  189.     [HideInInspector]
  190.     public bool pressButtonUpState;
  191.     [HideInInspector]
  192.     public Collider objToPickup;
  193.    
  194.     //zooming
  195.     private bool zoomBtnState = true;
  196.     private float zoomStopTime = 0.0f;//track time that zoom stopped to delay making aim reticle visible again
  197.     [HideInInspector]
  198.     public bool zoomed = false;
  199.     [HideInInspector]
  200.     public float zoomStart = -2.0f;
  201.     [HideInInspector]
  202.     public bool zoomStartState = false;
  203.     [HideInInspector]
  204.     public float zoomEnd = 0.0f;
  205.     [HideInInspector]
  206.     public bool zoomEndState = false;
  207.     private float zoomDelay = 0.4f;
  208.     [HideInInspector]
  209.     public bool dzAiming;
  210.    
  211.     //crosshair
  212.     [Tooltip("Enable or disable the aiming reticle.")]
  213.     public bool crosshairEnabled = true;
  214.     private bool crosshairVisibleState = true;
  215.     private bool crosshairTextureState = false;
  216.     [Tooltip("Set to true to display swap reticle when item under reticle will replace current weapon.")]
  217.     public bool useSwapReticle = true;
  218.     [Tooltip("The texture used for the aiming crosshair.")]
  219.     public Texture2D aimingReticle;
  220.     [Tooltip("The texture used for the hitmarker.")]
  221.     public Texture2D hitmarkerReticle;
  222.     [Tooltip("The texture used for the pick up crosshair.")]
  223.     public Texture2D pickupReticle;
  224.     [Tooltip("The texture used for when the weapon under reticle will replace current weapon.")]
  225.     public Texture2D swapReticle;
  226.     [Tooltip("The texture used for showing that weapon under reticle cannot be picked up.")]
  227.     public Texture2D noPickupReticle;
  228.     [Tooltip("The texture used for the pick up crosshair.")]
  229.     private Texture2D pickupTex;
  230.  
  231.     private Color pickupReticleColor = Color.white;
  232.     [HideInInspector]
  233.     public Color reticleColor = Color.white;
  234.     private Color hitmarkerColor = Color.white;
  235.     [Tooltip("Layers to include for crosshair raycast in hit detection.")]
  236.     public LayerMask rayMask;
  237.     [Tooltip("Distance that player can pickup and activate items.")]
  238.     public float reachDistance = 2.1f;
  239.  
  240.     private RaycastHit hit;
  241.     private RaycastHit hit2;
  242.     private Vector3 camCrosshairPos;
  243.     private Vector3 crosshairPos;
  244.     [HideInInspector]
  245.     public bool raycastCrosshair;
  246.    
  247.     //button and behavior states
  248.     private bool pickUpBtnState = true;
  249.     [HideInInspector]
  250.     public bool restarting = false;//to notify other scripts that level is restarting
  251.    
  252.     //sound effects
  253.     public AudioClip painLittle;
  254.     public AudioClip painBig;
  255.     public AudioClip painDrown;
  256.     public AudioClip gasp;
  257.     public AudioClip catchBreath;
  258.     public AudioClip die;
  259.     public AudioClip dieDrown;
  260.     public AudioClip jumpfx;
  261.     public AudioClip enterBulletTimeFx;
  262.     public AudioClip exitBulletTimeFx;
  263.     public AudioClip hitMarker;
  264.     [Tooltip("Particle effect to play when player blocks attack.")]
  265.     public GameObject blockParticles;
  266.     [Tooltip("Distance from camera to emit blocking particle effect.")]
  267.     public float blockParticlesPos;
  268.  
  269.     private AudioSource[]aSources;//access the audio sources attatched to this object as an array for playing player sound effects
  270.     [HideInInspector]
  271.     public AudioSource otherfx;
  272.     [HideInInspector]
  273.     public AudioSource hitmarkfx;
  274.     private bool bullettimefxstate;
  275.     [HideInInspector]
  276.     public bool blockState;
  277.     [HideInInspector]
  278.     public float blockAngle;
  279.     [HideInInspector]
  280.     public bool canBackstab;//true if player can backstab an unalerted NPC
  281.     private float moveCommandedTime;//last time that following NPCs were commanded to move (for command cooldown)
  282.    
  283.     [HideInInspector]
  284.     public bool menuDisplayed;
  285.     [HideInInspector]
  286.     public float menuTime;
  287.     [HideInInspector]
  288.     public float pauseTime;
  289.     [HideInInspector]
  290.     public bool paused;
  291.     private MainMenu MainMenuComponent;
  292.  
  293.     void Start (){ 
  294.  
  295.         if(removePrefabRoot){
  296.             GameObject prefabRoot = transform.parent.transform.gameObject;
  297.             transform.parent.transform.DetachChildren();
  298.             Destroy(prefabRoot);
  299.         }
  300.  
  301.         mainCamTransform = Camera.main.transform;
  302.         //set up external script references
  303.         IronsightsComponent = GetComponent<Ironsights>();
  304.         InputComponent = GetComponent<InputControl>();
  305.         FPSWalkerComponent = GetComponent<FPSRigidBodyWalker>();
  306.         WorldRecenterComponent = GetComponent<WorldRecenter>();
  307.         MouseLookComponent = mainCamTransform.parent.transform.GetComponent<SmoothMouseLook>();
  308.         CameraControlComponent = Camera.main.transform.GetComponent<CameraControl>();
  309.         weaponObj = CameraControlComponent.weaponObj;
  310.         WeaponEffectsComponent = weaponObj.GetComponent<WeaponEffects>();
  311.         PlayerWeaponsComponent = weaponObj.GetComponent<PlayerWeapons>();
  312.        
  313.         MainMenuComponent = Camera.main.transform.parent.transform.GetComponent<MainMenu>();
  314. //      MainMenuComponent.enabled = false;
  315.         menuDisplayed = false;
  316.        
  317.         NPCMgrObj = GameObject.Find("NPC Manager");
  318.         NPCRegistryComponent = NPCMgrObj.GetComponent<NPCRegistry>();
  319.            
  320.         aSources = GetComponents<AudioSource>();//Initialize audio source
  321.         otherfx = aSources[0] as AudioSource;
  322.         hitmarkfx = aSources[1] as AudioSource;
  323.         otherfx.spatialBlend = 0.0f;
  324.         hitmarkfx.spatialBlend = 0.0f;
  325.        
  326.         //Set time settings
  327.         Time.timeScale = 1.0f;
  328.         initialFixedTime =  Time.fixedDeltaTime;
  329.        
  330.         usePressTime = 0f;
  331.         useReleaseTime = -8f;
  332.        
  333.         //Physics Layer Management Setup (obsolete, no longer used)
  334.         //these are the layer numbers and their corresponding uses/names accessed by the FPS prefab
  335.         //  Weapon = 8;
  336.         //  Ragdoll = 9;
  337.         //  WorldCollision = 10;
  338.         //  Player = 11;
  339.         //  Objects = 12;
  340.         //  NPCs = 13;
  341.         //  GUICameraLayer = 14;
  342.         //  WorldGeometry = 15;
  343.         //  BulletMarks = 16;
  344.        
  345.         //player object collisions
  346.         Physics.IgnoreLayerCollision(11, 12);//no collisions between player object and misc objects like bullet casings
  347.         Physics.IgnoreLayerCollision (12, 12);//no collisions between bullet shells
  348.        
  349.         //weapon object collisions
  350.         Physics.IgnoreLayerCollision(8, 2);//
  351.         Physics.IgnoreLayerCollision(8, 13);//no collisions between weapon and NPCs
  352.         Physics.IgnoreLayerCollision(8, 12);//no collisions between weapon and Objects
  353.         Physics.IgnoreLayerCollision(8, 11);//no collisions between weapon and Player
  354.         Physics.IgnoreLayerCollision(8, 10);//no collisions between weapon and world collision
  355.  
  356.         //Call FadeAndLoadLevel fucntion with fadeIn argument set to true to tell the function to fade in (not fade out and (re)load level)
  357.         GameObject llf = Instantiate(levelLoadFadeObj) as GameObject;
  358.         llf.GetComponent<LevelLoadFade>().FadeAndLoadLevel(Color.black, 2.0f, true);
  359.        
  360.         //create instance of GUIText to display health amount on hud
  361.         healthGuiObjInstance = Instantiate(healthGuiObj,Vector3.zero,transform.rotation) as GameObject;
  362.         if(showHelpText){
  363.             //create instance of GUIText to display help text
  364.             helpGuiObjInstance = Instantiate(helpGuiObj,Vector3.zero,transform.rotation) as GameObject;
  365.         }
  366.         //create instance of GUITexture to display crosshair on hud
  367.         CrosshairGuiObjInstance = Instantiate(CrosshairGuiObj,new Vector3(0.5f,0.5f,0.0f),transform.rotation) as GameObject;
  368.         CrosshairGuiTexture = CrosshairGuiObjInstance.GetComponent<GUITexture>();
  369.         CrosshairGuiTexture.texture = aimingReticle;
  370.         hitmarkerGuiObjInstance = Instantiate(hitmarkerGuiObj,new Vector3(0.5f,0.5f,0.0f),transform.rotation) as GameObject;
  371.         hitmarkerGuiTexture = hitmarkerGuiObjInstance.GetComponent<GUITexture>();
  372.         hitmarkerGuiTexture.texture = hitmarkerReticle;
  373.         hitmarkerGuiTexture.enabled = false;
  374.         //set alpha of hand pickup crosshair
  375.         pickupReticleColor.a = 0.5f;
  376.         //set alpha of aiming reticule and make it 100% transparent if crosshair is disabled
  377.         if(crosshairEnabled){
  378.             reticleColor.a = 0.25f;
  379.             hitmarkerGuiTexture.color = hitmarkerColor;
  380.         }else{
  381.             //make alpha of aiming reticle zero/transparent
  382.             reticleColor.a = 0.0f;
  383.             //set alpha of aiming reticle at start to prevent it from showing, but allow item pickup hand reticle
  384.             CrosshairGuiTexture.color = reticleColor;
  385.             hitmarkerGuiTexture.color = reticleColor;
  386.         }
  387.        
  388.         //set reference for main color element of heath GUIText
  389.         HealthText = healthGuiObjInstance.GetComponent<HealthText>();
  390.         //set reference for shadow background color element of health GUIText
  391.         //this object is a child of the main health GUIText object, so access it as an array
  392.         HealthText2 = healthGuiObjInstance.GetComponentsInChildren<HealthText>();
  393.        
  394.         //initialize health amounts on GUIText objects
  395.         HealthText.healthGui = hitPoints;
  396.         HealthText2[1].healthGui = hitPoints;  
  397.         healthGuiText = HealthText.GetComponent<GUIText>();
  398.        
  399.         if(!showHealth){
  400.             healthGuiObjInstance.gameObject.SetActive(false);
  401.         }
  402.        
  403.         if(usePlayerHunger){
  404.             //create instance of GUIText to display hunger amount on hud
  405.             hungerGuiObjInstance = Instantiate(hungerGuiObj,Vector3.zero,transform.rotation) as GameObject;
  406.             //set reference for main color element of hunger GUIText
  407.             HungerText = hungerGuiObjInstance.GetComponent<HungerText>();
  408.             //set reference for shadow background color element of hunger GUIText
  409.             //this object is a child of the main hunger GUIText object, so access it as an array
  410.             HungerText2 = hungerGuiObjInstance.GetComponentsInChildren<HungerText>();
  411.            
  412.             //initialize hunger amounts on GUIText objects
  413.             HungerText.hungerGui = hungerPoints;
  414.             HungerText2[1].hungerGui = hungerPoints;   
  415.             HungerGUIText = HungerText.GetComponent<GUIText>();
  416.         }
  417.        
  418.         if(usePlayerThirst){
  419.             //create instance of GUIText to display thirst amount on hud
  420.             thirstGuiObjInstance = Instantiate(thirstGuiObj,Vector3.zero,transform.rotation) as GameObject;
  421.             //set reference for main color element of thirst GUIText
  422.             ThirstText = thirstGuiObjInstance.GetComponent<ThirstText>();
  423.             //set reference for shadow background color element of thirst GUIText
  424.             //this object is a child of the main thirst GUIText object, so access it as an array
  425.             ThirstText2 = thirstGuiObjInstance.GetComponentsInChildren<ThirstText>();
  426.            
  427.             //initialize thirst amounts on GUIText objects
  428.             ThirstText.thirstGui = thirstPoints;
  429.             ThirstText2[1].thirstGui = thirstPoints;
  430.             ThirstGUIText = ThirstText.GetComponent<GUIText>();
  431.         }
  432.        
  433.     }
  434.    
  435.     void LateUpdate () {
  436.    
  437.         if((MouseLookComponent.dzAiming || raycastCrosshair)
  438.         //&& WeaponBehaviorComponent.recoveryTime + WeaponBehaviorComponent.recoveryTimeAmt + 0.1f < Time.time
  439.         //&& !FPSWalkerComponent.sprintActive
  440.         /*&& !(FPSWalkerComponent.moving && FPSWalkerComponent.prone)*/){
  441.             if(!WeaponBehaviorComponent.unarmed
  442.             && Physics.Raycast(mainCamTransform.position, WeaponBehaviorComponent.weaponLookDirection, out hit, 100.0f, rayMask)){
  443.                 camCrosshairPos = Camera.main.WorldToScreenPoint(hit.point);
  444.                 crosshairPos = new Vector3(camCrosshairPos.x / Screen.width, camCrosshairPos.y / Screen.height, 0.0f);
  445.             }else{
  446.                 if(WeaponBehaviorComponent.unarmed){
  447.                     crosshairPos = new Vector3(0.5f, 0.5f, 0.0f);
  448.                 }else{
  449.                     camCrosshairPos = Camera.main.WorldToScreenPoint(WeaponBehaviorComponent.origin + WeaponPivotComponent.childTransform.forward * 2000.0f);
  450.                     crosshairPos = new Vector3(camCrosshairPos.x / Screen.width, camCrosshairPos.y / Screen.height, 0.0f);
  451.                 }
  452.             }
  453.         }else{
  454.             crosshairPos = new Vector3(0.5f, 0.5f, 0.0f);
  455.         }
  456.        
  457.         CrosshairGuiObjInstance.transform.position = Vector3.Lerp(CrosshairGuiObjInstance.transform.position, crosshairPos, (Time.smoothDeltaTime * 20.0f) * 4.0f);
  458.         hitmarkerGuiObjInstance.transform.position = CrosshairGuiObjInstance.transform.position;   
  459.         hitmarkerColor.a = 0.2f;
  460.         hitmarkerGuiTexture.color = hitmarkerColor;    
  461.        
  462.     }
  463.    
  464.     void Update (){
  465.    
  466.         //detect if menu display button was pressed
  467.         if (InputComponent.menuPress){
  468.             if(!menuDisplayed){
  469.                 MainMenuComponent.enabled = true;
  470.                 menuDisplayed = true;
  471.             }else{
  472.                 MainMenuComponent.enabled = false;
  473.                 paused = false;
  474.                 menuDisplayed = false;
  475.             }
  476.             if(Time.timeScale > 0.0f || paused){
  477.                 if(!paused){
  478.                     menuTime = Time.timeScale;
  479.                 }
  480.                 Time.timeScale = 0.0f;
  481.             }else{
  482.                 Time.timeScale = menuTime; 
  483.             }
  484.         }
  485.        
  486.         if(InputComponent.pausePress && !menuDisplayed){
  487.             if(Time.timeScale > 0.0f){
  488.                 paused = true;
  489.                 pauseTime = Time.timeScale;
  490.                 Time.timeScale = 0.0f;
  491.             }else{
  492.                 paused = false;
  493.                 Time.timeScale = pauseTime;
  494.             }
  495.         }
  496.            
  497.         if(allowBulletTime){//make bullet time an optional feature
  498.             if(InputComponent.bulletTimePress){//set bulletTimeActive to true or false based on button input
  499.                 if(!bulletTimeActive){
  500.                     FPSWalkerComponent.moveSpeedMult = Mathf.Clamp(sloMoMoveSpeed, 1f, sloMoMoveSpeed);
  501.                     bulletTimeActive = true;
  502.                 }else{
  503.                     FPSWalkerComponent.moveSpeedMult = 1.0f;
  504.                     bulletTimeActive = false;
  505.                 }
  506.             }
  507.                    
  508.             otherfx.pitch = Time.timeScale;//sync pitch of bullet time sound effects with Time.timescale
  509.             hitmarkfx.pitch = Time.timeScale;
  510.        
  511.             if(Time.timeScale > 0 && !restarting){//decrease or increase Time.timescale when bulletTimeActive is true
  512.                 Time.fixedDeltaTime = initialFixedTime * Time.timeScale;
  513.                 if(bulletTimeActive){
  514.                     if(!bullettimefxstate){
  515.                         otherfx.clip = enterBulletTimeFx;
  516.                         otherfx.PlayOneShot(otherfx.clip, 1.0f);//play enter bullet time sound effect
  517.                         bullettimefxstate = true;
  518.                     }
  519.                     Time.timeScale = Mathf.MoveTowards(Time.timeScale, bulletTimeSpeed, Time.deltaTime * 3.0f);
  520.                 }else{
  521.                     if(bullettimefxstate){
  522.                         otherfx.clip = exitBulletTimeFx;
  523.                         otherfx.PlayOneShot(otherfx.clip, 1.0f);//play exit bullet time sound effect
  524.                         FPSWalkerComponent.moveSpeedMult = 1.0f;
  525.                         bullettimefxstate = false;
  526.                     }
  527.                     if(1.0f - Mathf.Abs(Time.timeScale) > 0.05f){//make sure that timescale returns to exactly 1.0f
  528.                         Time.timeScale = Mathf.MoveTowards(Time.timeScale, 1.0f, Time.deltaTime * 3.0f);
  529.                     }else{
  530.                         Time.timeScale = 1.0f;
  531.                     }
  532.                 }
  533.             }
  534.         }
  535.        
  536.         //set zoom mode to toggle, hold, or both, based on inspector setting
  537.         switch (IronsightsComponent.zoomMode){
  538.             case Ironsights.zoomType.both:
  539.                 zoomDelay = 0.4f;
  540.             break;
  541.             case Ironsights.zoomType.hold:
  542.                 zoomDelay = 0.0f;
  543.             break;
  544.             case Ironsights.zoomType.toggle:
  545.                 zoomDelay = 999.0f;
  546.             break;
  547.         }
  548.        
  549.         //regenerate player health if regenerateHealth var is true
  550.         if(regenerateHealth){
  551.             if(hitPoints < maxRegenHealth && timeLastDamaged + healthRegenDelay < Time.time){
  552.                 HealPlayer(healthRegenRate * Time.deltaTime);  
  553.             }
  554.         }
  555.        
  556.         //apply player hunger if usePlayerHunger var is true
  557.         if(usePlayerHunger){
  558.             thirstGuiObjInstance.SetActive(true);
  559.             //increase player hunger
  560.             if(lastHungerTime + hungerInterval < Time.time){
  561.                 UpdateHunger(1.0f);
  562.             }
  563.             //calculate and apply starvation damage to player
  564.             if(hungerPoints == maxHungerPoints
  565.             && lastStarveTime + starveInterval < Time.time
  566.             && hitPoints > 0.0f){
  567.                 //use a negative heal amount to prevent unneeded damage effects of ApplyDamage function
  568.                 HealPlayer(starveDmgAmt, true);//
  569.                 //fade screen red when taking starvation damage
  570.                 GameObject pf = Instantiate(painFadeObj) as GameObject;//Create instance of painFadeObj
  571.                 pf.GetComponent<PainFade>().FadeIn(PainColor, painTexture, 0.75f);//Call FadeIn function in painFadeObj to fade screen red when damage taken
  572.                 //Call Die function if player's hitpoints have been depleted
  573.                 if (hitPoints < 1.0f){
  574.                     SendMessage("Die");//use SendMessage() to allow other script components on this object to detect player death
  575.                 }
  576.                 //update starvation timers
  577.                 timeLastDamaged = Time.time;
  578.                 lastStarveTime = Time.time;
  579.             }
  580.            
  581.         }else{
  582.             if(thirstGuiObjInstance){
  583.                 thirstGuiObjInstance.SetActive(false);
  584.             }
  585.         }
  586.        
  587.         //apply player thirst if usePlayerThirst var is true
  588.         if(usePlayerThirst){
  589.             hungerGuiObjInstance.SetActive(true);
  590.             //increase player hunger
  591.             if(lastThirstTime + thirstInterval < Time.time){
  592.                 UpdateThirst(1.0f);
  593.             }
  594.             //calculate and apply starvation damage to player
  595.             if(thirstPoints == maxThirstPoints
  596.             && lastThirstDmgTime + thirstDmgInterval < Time.time
  597.             && hitPoints > 0.0f){
  598.                 //use a negative heal amount to prevent unneeded damage effects of ApplyDamage function
  599.                 HealPlayer(thirstDmgAmt, true);
  600.                 //fade screen red when taking starvation damage
  601.                 GameObject pf = Instantiate(painFadeObj) as GameObject;//Create instance of painFadeObj
  602.                 pf.GetComponent<PainFade>().FadeIn(PainColor, painTexture, 0.75f);//Call FadeIn function in painFadeObj to fade screen red when damage taken
  603.                 //Call Die function if player's hitpoints have been depleted
  604.                 if (hitPoints < 1.0f){
  605.                     Die();
  606.                 }
  607.                 //update starvation timers
  608.                 timeLastDamaged = Time.time;
  609.                 lastThirstDmgTime = Time.time;
  610.             }
  611.            
  612.         }else{
  613.             if(hungerGuiObjInstance){
  614.                 hungerGuiObjInstance.SetActive(false);
  615.             }
  616.         }
  617.        
  618.         WeaponBehavior WeaponBehaviorComponent = PlayerWeaponsComponent.CurrentWeaponBehaviorComponent;
  619.        
  620.         //toggle or hold zooming state by determining if zoom button is pressed or held
  621.         if(InputComponent.zoomHold
  622.         && WeaponBehaviorComponent.canZoom
  623.         && !blockState
  624.         && !IronsightsComponent.reloading
  625.         && !FPSWalkerComponent.proneMove//no zooming while crawling
  626.         && !FPSWalkerComponent.hideWeapon){
  627.             if(!zoomStartState){
  628.                 zoomStart = Time.time;//track time that zoom button was pressed
  629.                 zoomStartState = true;//perform these actions only once
  630.                 zoomEndState = false;
  631.                 if(zoomEnd - zoomStart < zoomDelay * Time.timeScale){//if button is tapped, toggle zoom state
  632.                     if(!zoomed){
  633.                         zoomed = true;
  634.                     }else{
  635.                         zoomed = false;
  636.                     }
  637.                 }
  638.             }
  639.         }else{
  640.             if(!InputComponent.zoomHold){blockState = false;}//reset block after a hit, so player needs to press block/zoom button again
  641.             if(!zoomEndState){
  642.                 zoomEnd = Time.time;//track time that zoom button was released
  643.                 zoomEndState = true;
  644.                 zoomStartState = false;
  645.                 if(zoomEnd - zoomStart > zoomDelay * Time.timeScale){//if releasing zoom button after holding it down, stop zooming
  646.                     zoomed = false;
  647.                 }
  648.             }
  649.         }
  650.        
  651.         //cancel zooming while crawling
  652.         if(FPSWalkerComponent.proneMove){
  653.             zoomEndState = true;
  654.             zoomStartState = false;
  655.             zoomed = false;
  656.         }
  657.        
  658.         //track when player stopped zooming to allow for delay of reticle becoming visible again
  659.         if (zoomed){
  660.             zoomBtnState = false;//only perform this action once per button press
  661.         }else{
  662.             if(!zoomBtnState){
  663.                 zoomStopTime = Time.time;
  664.                 zoomBtnState = true;
  665.             }
  666.         }
  667.        
  668.         //scale crosshair size with screen size by crosshairSize value
  669.         if(oldWidth != Screen.width || crossRect.width != Screen.width * crosshairSize){
  670.             crossRect.width = Screen.width * crosshairSize;
  671.             crossRect.height = Screen.width * crosshairSize;
  672.             crossRect.x = -crossRect.width * 0.5f;
  673.             crossRect.y = -crossRect.height * 0.5f;
  674.             CrosshairGuiTexture.pixelInset = crossRect;
  675.             hitmarkerGuiTexture.pixelInset = crossRect;
  676.             oldWidth = Screen.width;
  677.         }
  678.        
  679.         UpdateHitmarker();
  680.        
  681.         //enable and disable crosshair based on various states like reloading and zooming
  682.         if((IronsightsComponent.reloading || (zoomed && (!dzAiming || WeaponBehaviorComponent.zoomIsBlock) && !WeaponBehaviorComponent.showZoomedCrosshair))
  683.         && !CameraControlComponent.thirdPersonActive){
  684.             //don't disable reticle if player is using a melee weapon or if player is unarmed
  685.             if((WeaponBehaviorComponent.meleeSwingDelay == 0 || WeaponBehaviorComponent.zoomIsBlock) && !WeaponBehaviorComponent.unarmed){
  686.                 if(crosshairVisibleState){
  687.                     //disable the GUITexture element of the instantiated crosshair object
  688.                     //and set state so this action will only happen once.
  689.                     CrosshairGuiTexture.enabled = false;
  690.                     crosshairVisibleState = false;
  691.                 }
  692.             }
  693.         }else{
  694.             //Because of the method that is used for non magazine reloads, an additional check is needed here
  695.             //to make the reticle appear after the last bullet reload time has elapsed. Proceed with no check
  696.             //for magazine reloads.
  697.             if((WeaponBehaviorComponent.bulletsPerClip != WeaponBehaviorComponent.bulletsToReload
  698.                 && WeaponBehaviorComponent.reloadLastStartTime + WeaponBehaviorComponent.reloadLastTime < Time.time)
  699.             || WeaponBehaviorComponent.bulletsPerClip == WeaponBehaviorComponent.bulletsToReload){
  700.                 //allow a delay before enabling crosshair again to let the gun return to neutral position
  701.                 //by checking the zoomStopTime value
  702.                 if(zoomStopTime + 0.2f < Time.time){
  703.                     if(!crosshairVisibleState){
  704.                         CrosshairGuiTexture.enabled = true;
  705.                         crosshairVisibleState = true;
  706.                     }
  707.                 }
  708.             }
  709.         }
  710.        
  711.         if(crosshairEnabled){
  712.             if(WeaponBehaviorComponent.showAimingCrosshair){
  713.                 if(!WeaponPivotComponent.deadzoneZooming){
  714.                     if(!WeaponPivotComponent.deadzoneLooking){
  715.                         reticleColor.a = 0.25f;
  716.                     }else{
  717.                         reticleColor.a = 0.5f;
  718.                     }
  719.                 }else{
  720.                     if(!CameraControlComponent.thirdPersonActive){
  721.                         if(zoomed){
  722.                             reticleColor.a = 0.5f;
  723.                         }else{
  724.                             if(WeaponPivotComponent.swayLeadingMode){
  725.                                 reticleColor.a = 0.25f;
  726.                             }else{
  727.                                 reticleColor.a = 0.0f;//no crosshair for goldeneye/perfect dark style, non-zoomed aiming
  728.                             }
  729.                         }
  730.                     }else{
  731.                         reticleColor.a = 0.25f;
  732.                     }
  733.                 }
  734.                 CrosshairGuiTexture.color = reticleColor;
  735.             }else{
  736.                 //make alpha of aiming reticle zero/transparent
  737.                 reticleColor.a = 0.0f;
  738.                 //set alpha of aiming reticle at start to prevent it from showing, but allow item pickup hand reticle
  739.                 CrosshairGuiTexture.color = reticleColor;
  740.             }
  741.         }else{
  742.             reticleColor.a = 0.0f;
  743.             CrosshairGuiTexture.color = reticleColor;
  744.         }
  745.                
  746.         /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  747.         //Pick up or activate items
  748.         /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  749.        
  750.         if(InputComponent.useHold){
  751.             if(!useState){
  752.                 usePressTime = Time.time;
  753.                 objToPickup = hit.collider;
  754.                 useState = true;
  755.             }
  756.         }else{
  757.             if(useState){
  758.                 useReleaseTime = Time.time;
  759.                 useState = false;
  760.             }
  761.             pressButtonUpState = false;
  762.         }
  763.            
  764.         if(!IronsightsComponent.reloading//no item pickup when reloading
  765.         && !WeaponBehaviorComponent.lastReload//no item pickup when when reloading last round in non magazine reload
  766.         && !PlayerWeaponsComponent.switching//no item pickup when switching weapons
  767.         && !FPSWalkerComponent.holdingObject//don't pick up objects if player is dragging them
  768.         && (!FPSWalkerComponent.canRun || FPSWalkerComponent.inputY == 0)//no item pickup when sprinting
  769.             //there is a small delay between the end of canRun and the start of sprintSwitching (in PlayerWeapons script),
  770.             //so track actual time that sprinting stopped to avoid the small time gap where the pickup hand shows briefly
  771.         && ((FPSWalkerComponent.sprintStopTime + 0.4f) < Time.time)){
  772.             //raycast a line from the main camera's origin using a point extended forward from camera position/origin as a target to get the direction of the raycast
  773.             //and scale the distance of the raycast based on the playerHeightMod value in the FPSRigidbodyWalker script
  774.             if((!CameraControlComponent.thirdPersonActive && Physics.Raycast(mainCamTransform.position,
  775.                                                    WeaponBehaviorComponent.weaponLookDirection,
  776.                                                    out hit,
  777.                                                    reachDistance + FPSWalkerComponent.playerHeightMod,
  778.                                                    rayMask))
  779.             //thirdperson item detection for use button and crosshair
  780.             ||(CameraControlComponent.thirdPersonActive && Physics.Raycast(mainCamTransform.position + mainCamTransform.forward * (CameraControlComponent.zoomDistance + CameraControlComponent.currentDistance + 0.5f),
  781.                                                  mainCamTransform.forward, out hit,
  782.                                                  reachDistance + FPSWalkerComponent.playerHeightMod,
  783.                                                  rayMask))
  784.             ){
  785.                
  786.                 //Detect if player can backstab NPCs
  787.                 if(WeaponBehaviorComponent.meleeSwingDelay > 0 && hit.collider.gameObject.layer == 13){
  788.                     if(hit.collider.gameObject.GetComponent<AI>() || hit.collider.gameObject.GetComponent<LocationDamage>()){
  789.                         if(hit.collider.gameObject.GetComponent<AI>()){
  790.                             AIComponent = hit.collider.gameObject.GetComponent<AI>();
  791.                         }else{
  792.                             AIComponent = hit.collider.gameObject.GetComponent<LocationDamage>().AIComponent;
  793.                         }
  794.                         if(AIComponent.playerIsBehind && AIComponent.CharacterDamageComponent.hitPoints > 0){
  795.                             canBackstab = true;  
  796.                         }else{
  797.                             canBackstab = false;  
  798.                         }
  799.                     }else{
  800.                         canBackstab = false;  
  801.                     }
  802.                 }else{
  803.                     canBackstab = false;
  804.                 }
  805.                
  806.                 if(hit.collider.gameObject.tag == "Usable"){//if the object hit by the raycast is a pickup item and has the "Usable" tag
  807.                    
  808.                     if (pickUpBtnState && usePressTime - useReleaseTime < 0.4f && usePressTime + 0.4f > Time.time && objToPickup == hit.collider){
  809.                         //run the PickUpItem function in the pickup object's script
  810.                         hit.collider.SendMessageUpwards("PickUpItem", transform.gameObject, SendMessageOptions.DontRequireReceiver);
  811.                         //run the ActivateObject function of this object's script if it has the "Usable" tag
  812.                         hit.collider.SendMessageUpwards("ActivateObject", SendMessageOptions.DontRequireReceiver);
  813.                         pickUpBtnState = false;
  814.                         FPSWalkerComponent.cancelSprint = true;
  815.                         usePressTime = -8f;
  816.                         objToPickup = null;
  817.                     }
  818.                    
  819.                     //determine if pickup item is using a custom pickup reticle and if so set pickupTex to custom reticle
  820.                     if(pickUpBtnState){//check pickUpBtnState to prevent reticle from briefly showing custom/general pickup icon briefly when picking up last weapon before maxWeapons are obtained
  821.                        
  822.                         //determine if item under reticle is a weapon pickup
  823.                         if(hit.collider.gameObject.GetComponent<WeaponPickup>()){
  824.                             //set up external script references
  825.                             WeaponBehavior PickupWeaponBehaviorComponent = PlayerWeaponsComponent.weaponOrder[hit.collider.gameObject.GetComponent<WeaponPickup>().weaponNumber].GetComponent<WeaponBehavior>();
  826.                             WeaponPickup WeaponPickupComponent = hit.collider.gameObject.GetComponent<WeaponPickup>();
  827.                            
  828.                             if(PlayerWeaponsComponent.totalWeapons == PlayerWeaponsComponent.maxWeapons//player has maximum weapons
  829.                             && PickupWeaponBehaviorComponent.addsToTotalWeaps){//weapon adds to total inventory
  830.                                
  831.                                 //player does not have weapon under reticle
  832.                                 if(!PickupWeaponBehaviorComponent.haveWeapon
  833.                                 //and weapon under reticle hasn't been picked up from an item with removeOnUse set to false
  834.                                 && !PickupWeaponBehaviorComponent.dropWillDupe){   
  835.                                    
  836.                                     if(!useSwapReticle){//if useSwapReticle is true, display swap reticle when item under reticle will replace current weapon
  837.                                         if(WeaponPickupComponent.weaponPickupReticle){
  838.                                             //display custom weapon pickup reticle if the weapon item has one defined
  839.                                             pickupTex = WeaponPickupComponent.weaponPickupReticle; 
  840.                                         }else{
  841.                                             //weapon has no custom pickup reticle, just show general pickup reticle
  842.                                             pickupTex = pickupReticle;
  843.                                         }
  844.                                     }else{
  845.                                         //display weapon swap reticle if player has max weapons and can swap held weapon for pickup under reticle
  846.                                         pickupTex = swapReticle;
  847.                                     }
  848.                                    
  849.                                 }else{
  850.                                    
  851.                                     //weapon under reticle is not removed on use and is in player's inventory, so show cantPickup reticle
  852.                                     if(!WeaponPickupComponent.removeOnUse){
  853.                                        
  854.                                         pickupTex = noPickupReticle;
  855.                                        
  856.                                     }else{//weapon is removed on use, so show standard or custom pickup reticle
  857.                                        
  858.                                         if(WeaponPickupComponent.weaponPickupReticle){
  859.                                             //display custom weapon pickup reticle if the weapon item has one defined
  860.                                             pickupTex = WeaponPickupComponent.weaponPickupReticle; 
  861.                                         }else{
  862.                                             //weapon has no custom pickup reticle, just show general pickup reticle
  863.                                             pickupTex = pickupReticle;
  864.                                         }
  865.                                        
  866.                                     }
  867.                                    
  868.                                 }
  869.                             }else{//total weapons not at maximum and weapon under reticle does not add to inventory
  870.                                
  871.                                 if(!PickupWeaponBehaviorComponent.haveWeapon
  872.                                 && !PickupWeaponBehaviorComponent.dropWillDupe
  873.                                 || WeaponPickupComponent.removeOnUse){
  874.                                    
  875.                                     if(WeaponPickupComponent.weaponPickupReticle){
  876.                                         //display custom weapon pickup reticle if the weapon item has one defined
  877.                                         pickupTex = WeaponPickupComponent.weaponPickupReticle; 
  878.                                     }else{
  879.                                         //weapon has no custom pickup reticle, just show general pickup reticle
  880.                                         pickupTex = pickupReticle;
  881.                                     }
  882.                                    
  883.                                 }else{
  884.                                     pickupTex = noPickupReticle;
  885.                                 }
  886.                                
  887.                             }
  888.                         //determine if item under reticle is a health pickup   
  889.                         }else if(hit.collider.gameObject.GetComponent<HealthPickup>()){
  890.                             //set up external script references
  891.                             HealthPickup HealthPickupComponent = hit.collider.gameObject.GetComponent<HealthPickup>();
  892.                            
  893.                             if(HealthPickupComponent.healthPickupReticle){
  894.                                 pickupTex = HealthPickupComponent.healthPickupReticle; 
  895.                             }else{
  896.                                 pickupTex = pickupReticle;
  897.                             }
  898.                         //determine if item under reticle is an ammo pickup
  899.                         }else if(hit.collider.gameObject.GetComponent<AmmoPickup>()){
  900.                             //set up external script references
  901.                             AmmoPickup AmmoPickupComponent = hit.collider.gameObject.GetComponent<AmmoPickup>();
  902.                            
  903.                             if(AmmoPickupComponent.ammoPickupReticle){
  904.                                 pickupTex = AmmoPickupComponent.ammoPickupReticle; 
  905.                             }else{
  906.                                 pickupTex = pickupReticle;
  907.                             }
  908.                         }else{
  909.                             pickupTex = pickupReticle;
  910.                         }
  911.                     }
  912.                    
  913.                     UpdateReticle(false);//show pickupReticle if raycast hits a pickup item
  914.  
  915.                 }else{
  916.                     objToPickup = null;//cancel use press if player moves away
  917.                     if(hit.collider.gameObject.layer == 13){//switch to pickup reticle if this NPC can be interacted with
  918.                         if(hit.collider.gameObject.GetComponent<AI>()
  919.                         || hit.collider.gameObject.GetComponent<LocationDamage>()){
  920.                             if(hit.collider.gameObject.GetComponent<AI>()){
  921.                                 AIComponent = hit.collider.gameObject.GetComponent<AI>();
  922.                             }else{
  923.                                 AIComponent = hit.collider.gameObject.GetComponent<LocationDamage>().AIComponent;
  924.                             }
  925.                             if(AIComponent.factionNum == 1 && AIComponent.followOnUse && AIComponent.enabled){
  926.                                 pickupTex = pickupReticle;
  927.                                 UpdateReticle(false);
  928.                                 if (pickUpBtnState && InputComponent.useHold){
  929.                                     AIComponent.CommandNPC();//command NPC to follow or stay put
  930.                                     pickUpBtnState = false;
  931.                                     FPSWalkerComponent.cancelSprint = true;
  932.                                 }
  933.                             }else{
  934.                                 UpdateReticle(true);//show aiming reticle crosshair if item is not a pickup item
  935.                             }
  936.                         }else{
  937.                             if(crosshairTextureState){
  938.                                 UpdateReticle(true);//show aiming reticle crosshair if item is not a pickup item
  939.                             }
  940.                         }
  941.                     }else{
  942.                         if(crosshairTextureState){
  943.                             UpdateReticle(true);//show aiming reticle crosshair if item is not a pickup item
  944.                         }
  945.                     }
  946.                 }
  947.             }else{
  948.                 canBackstab = false;
  949.                 if(crosshairTextureState){
  950.                     UpdateReticle(true);//show aiming reticle crosshair if raycast hits nothing
  951.                 }
  952.                 //Command NPCs to move to location under crosshair
  953.                 if(moveCommandedTime + 0.5f < Time.time &&
  954.                 ((!CameraControlComponent.thirdPersonActive && Physics.Raycast(mainCamTransform.position, WeaponBehaviorComponent.weaponLookDirection, out hit2, 500f, rayMask))
  955.                 ||(CameraControlComponent.thirdPersonActive && Physics.Raycast(mainCamTransform.position, mainCamTransform.forward, out hit2, 500f, rayMask)))){
  956.                     if(hit2.collider.gameObject.layer == 10 || hit2.collider.gameObject.layer == 0){
  957.                         if (pickUpBtnState && InputComponent.useHold){
  958.                             NPCRegistryComponent.MoveFolowingNpcs(hit2.point);
  959.                             moveCommandedTime = Time.time;
  960.                             pickUpBtnState = false;
  961.                         }
  962.                     }
  963.                 }
  964.             }
  965.         }else{
  966.             canBackstab = false;
  967.             if(crosshairTextureState){
  968.                 UpdateReticle(true);//show aiming reticle crosshair if reloading, switching weapons, or sprinting
  969.             }
  970.         }
  971.        
  972.         //only register one press of E key to make player have to press button again to pickup items instead of holding E
  973.         if (InputComponent.useHold){
  974.             pickUpBtnState = false;
  975.         }else{
  976.             pickUpBtnState = true; 
  977.         }
  978.    
  979.     }
  980.    
  981. //  void OnDrawGizmos() {
  982.         //draw red dot at crosshair raycast position
  983. //      Gizmos.color = Color.red;
  984. //      Gizmos.DrawSphere(hit2.point, 0.2f);
  985. //  }
  986.    
  987.     //set reticle type based on the boolean value passed to this function
  988.     public void UpdateReticle( bool reticleType ){
  989.         if(!reticleType){
  990.             CrosshairGuiTexture.texture = pickupTex;
  991.             CrosshairGuiTexture.color = pickupReticleColor;
  992.             crosshairTextureState = true;
  993.         }else{
  994.             CrosshairGuiTexture.texture = aimingReticle;
  995.             CrosshairGuiTexture.color = reticleColor;
  996.             crosshairTextureState = false; 
  997.         }
  998.     }
  999.    
  1000.     void UpdateHitmarker(){
  1001.         if(hitTime + 0.3f > Time.time){
  1002.             if(!hitMarkerState){
  1003.                 if(WeaponBehaviorComponent.meleeSwingDelay == 0 && !WeaponBehaviorComponent.meleeActive){
  1004.                     hitmarkerGuiTexture.enabled = true;
  1005.                     hitmarkfx.clip = hitMarker;
  1006.                     hitmarkfx.PlayOneShot(hitmarkfx.clip, 1.0f);
  1007.                     hitMarkerState = true;
  1008.                 }
  1009.             }
  1010.         }else{
  1011.             if(hitMarkerState){
  1012.                 hitMarkerState = false;
  1013.             }
  1014.             hitmarkerGuiTexture.enabled = false;
  1015.         }
  1016.     }
  1017.    
  1018.     public void UpdateHitTime(){
  1019.         hitTime = Time.time;//used for hitmarker
  1020.         hitMarkerState = false;
  1021.     }
  1022.    
  1023.     //Activate bullet time for a specific duration
  1024.     public IEnumerator ActivateBulletTime (float duration){
  1025.         if(!bulletTimeActive){
  1026.             bulletTimeActive = true;
  1027.         }else{
  1028.             yield break;
  1029.         }
  1030.         float startTime = Time.time;
  1031.         while(true){
  1032.             if(startTime + duration < Time.time){
  1033.                 bulletTimeActive = false;
  1034.                 yield break;
  1035.             }
  1036.             yield return new WaitForSeconds(0.1f);
  1037.         }  
  1038.     }
  1039.    
  1040.     /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1041.     //Update player attributes
  1042.     /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1043.    
  1044.     //add hitpoints to player health
  1045.     public void HealPlayer( float healAmt, bool isHungryThirsty = false ){
  1046.            
  1047.         if (hitPoints < 1.0f){//Don't add health if player is dead
  1048.             return;
  1049.         }
  1050.        
  1051.         //Apply healing
  1052.         if(hitPoints + healAmt > maximumHitPoints){
  1053.             hitPoints = maximumHitPoints;
  1054.         }else{
  1055.             //Call Die function if player's hitpoints have been depleted
  1056.             if(healAmt < 0){
  1057.                 if(!isHungryThirsty){
  1058.                     ApplyDamage(healAmt);//allow items that cause damage when consumed
  1059.                 }else{
  1060.                     hitPoints += healAmt;//waste away from hunger or thirst
  1061.                 }
  1062.             }else{
  1063.                 hitPoints += healAmt;
  1064.             }
  1065.         }
  1066.            
  1067.         //set health hud value to hitpoints remaining
  1068.         HealthText.healthGui = Mathf.Round(hitPoints);
  1069.         HealthText2[1].healthGui = Mathf.Round(hitPoints);
  1070.            
  1071.         //change color of hud health element based on hitpoints remaining
  1072.         if (hitPoints <= 25.0f){
  1073.             healthGuiText.material.color = Color.red;
  1074.         }else if (hitPoints <= 40.0f){
  1075.             healthGuiText.material.color = Color.yellow;   
  1076.         }else{
  1077.             healthGuiText.material.color = HealthText.textColor;   
  1078.         }
  1079.  
  1080.     }
  1081.    
  1082.     //update the hunger amount for the player
  1083.     public void UpdateHunger( float hungerAmt ){
  1084.        
  1085.         if (hitPoints < 1.0f){//Don't add hunger if player is dead
  1086.             return;
  1087.         }
  1088.        
  1089.         //Apply hungerAmt
  1090.         if(hungerPoints + hungerAmt > maxHungerPoints){
  1091.             hungerPoints = maxHungerPoints;
  1092.         }else{
  1093.             hungerPoints += hungerAmt;
  1094.         }
  1095.        
  1096.         hungerPoints = Mathf.Clamp(hungerPoints, 0.0f, hungerPoints);
  1097.            
  1098.         //set hunger hud value to hunger points remaining
  1099.         HungerText.hungerGui = Mathf.Round(hungerPoints);
  1100.         HungerText2[1].hungerGui = Mathf.Round(hungerPoints);
  1101.            
  1102.         //change color of hud hunger element based on hunger points
  1103.         if (hungerPoints <= 65.0f){
  1104.             HungerGUIText.material.color = HungerText.textColor;
  1105.         }else if (hungerPoints <= 85.0f){
  1106.                 HungerGUIText.material.color = Color.yellow;   
  1107.         }else{
  1108.             HungerGUIText.material.color = Color.red;  
  1109.         }
  1110.        
  1111.         lastHungerTime = Time.time;
  1112.     }
  1113.    
  1114.     //update the thirst amount for the player
  1115.     public void UpdateThirst( float thirstAmt ){
  1116.        
  1117.         if (hitPoints < 1.0f){//Don't add thirst if player is dead
  1118.             return;
  1119.         }
  1120.        
  1121.         //Apply thirstAmt
  1122.         if(thirstPoints + thirstAmt > maxThirstPoints){
  1123.             thirstPoints = maxThirstPoints;
  1124.         }else{
  1125.             thirstPoints += thirstAmt;
  1126.         }
  1127.        
  1128.         thirstPoints = Mathf.Clamp(thirstPoints, 0.0f, thirstPoints);
  1129.            
  1130.         //set thirst hud value to thirst points remaining
  1131.         ThirstText.thirstGui = Mathf.Round(thirstPoints);
  1132.         ThirstText2[1].thirstGui = Mathf.Round(thirstPoints);
  1133.            
  1134.         //change color of hud thirst element based on thirst points
  1135.         if (thirstPoints <= 65.0f){
  1136.             ThirstGUIText.material.color = ThirstText.textColor;
  1137.         }else if (thirstPoints <= 85.0f){
  1138.                 ThirstGUIText.material.color = Color.yellow;   
  1139.         }else{
  1140.             ThirstGUIText.material.color = Color.red;  
  1141.         }
  1142.        
  1143.         lastThirstTime = Time.time;
  1144.     }
  1145.    
  1146.     //remove hitpoints from player health
  1147.     public void ApplyDamage ( float damage, Transform attacker = null, bool isMeleeAttack = false){
  1148.  
  1149.         float appliedPainKickAmt;
  1150.            
  1151.         if (hitPoints < 1.0f){//Don't apply damage if player is dead
  1152.             if(!showHpUnderZero){hitPoints = 0.0f;}
  1153.             return;
  1154.         }
  1155.        
  1156.         //detect if attacker is inside player block zone
  1157.         if(attacker != null
  1158.         && WeaponBehaviorComponent.zoomIsBlock
  1159.         && WeaponBehaviorComponent.blockDefenseAmt > 0f
  1160.         && zoomed
  1161.         && ((WeaponBehaviorComponent.onlyBlockMelee && isMeleeAttack) || !WeaponBehaviorComponent.onlyBlockMelee)
  1162.         && WeaponBehaviorComponent.shootStartTime + WeaponBehaviorComponent.fireRate < Time.time){
  1163.        
  1164.             Vector3 toTarget = (attacker.position - transform.position).normalized;
  1165.             blockAngle = Vector3.Dot(toTarget, transform.forward);
  1166.            
  1167.             if(Vector3.Dot(toTarget, transform.forward) > WeaponBehaviorComponent.blockCoverage){
  1168.            
  1169.                 damage *= 1f - WeaponBehaviorComponent.blockDefenseAmt;
  1170.                 otherfx.clip = WeaponBehaviorComponent.blockSound;
  1171.                 otherfx.PlayOneShot(otherfx.clip, 1.0f);
  1172.                
  1173.                 if(blockParticles){
  1174.                     blockParticles.transform.position = Camera.main.transform.position + Camera.main.transform.forward * (blockParticlesPos + CameraControlComponent.zoomDistance + CameraControlComponent.currentDistance);
  1175.                     if(blockParticles.GetComponent<ParticleEmitter>()){blockParticles.GetComponent<ParticleEmitter>().Emit();}
  1176.                     foreach (Transform child in blockParticles.transform){//emit all particles in the particle effect game object group stored in blockParticles var
  1177.                         child.GetComponent<ParticleEmitter>().Emit();//emit the particle(s)
  1178.                     }
  1179.                 }
  1180.                 blockState = true;
  1181.             }
  1182.         }
  1183.        
  1184.         timeLastDamaged = Time.time;
  1185.  
  1186.         Quaternion painKickRotation;//Set up rotation for pain view kicks
  1187.         int painKickUpAmt = 0;
  1188.         int painKickSideAmt = 0;
  1189.  
  1190.         if(!invulnerable){
  1191.             hitPoints -= damage;//Apply damage
  1192.             Monstr.transform.position = Vector3.Lerp (Monstr.position, Point1.position, speedt);
  1193.  
  1194.             //GameObject.Find ("ZombieNPC").GetComponent<AI> ().StartCoroutine (patrol ());
  1195.             //GameObject.Find ("ZombieNPC").GetComponent<AI> ().Patrol ();
  1196.  
  1197.             GameObject.Find ("ZombieNPC").GetComponent<AI> ().targetVisible = false;
  1198.  
  1199.         }
  1200.    
  1201.         //set health hud value to hitpoints remaining
  1202.         HealthText.healthGui = Mathf.Round(hitPoints);
  1203.         HealthText2[1].healthGui = Mathf.Round(hitPoints);
  1204.            
  1205.         //change color of hud health element based on hitpoints remaining
  1206.         if (hitPoints <= 25.0f){
  1207.             healthGuiText.material.color = Color.red;
  1208.         }else if (hitPoints <= 40.0f){
  1209.             healthGuiText.material.color = Color.yellow;   
  1210.         }else{
  1211.             healthGuiText.material.color = HealthText.textColor;   
  1212.         }
  1213.        
  1214.         if(!blockState){
  1215.             GameObject pf = Instantiate(painFadeObj) as GameObject;//Create instance of painFadeObj
  1216.             painFadeColor = PainColor;
  1217.             painFadeColor.a = (damage / 5.0f);//fade pain overlay based on damage amount
  1218.             pf.GetComponent<PainFade>().FadeIn(painFadeColor, painTexture, 0.75f);//Call FadeIn function in painFadeObj to fade screen red when damage taken
  1219.         }
  1220.            
  1221.         if(!FPSWalkerComponent.holdingBreath){
  1222.             //Play pain sound when getting hit
  1223.             if(!blockState){//don't play hit sound if blocking attack
  1224.                 if (Time.time > gotHitTimer && painBig && painLittle) {
  1225.                     // Play a big pain sound
  1226.                     if (hitPoints < 40.0f || damage > 30.0f) {
  1227.                         otherfx.clip = painBig;
  1228.                         otherfx.PlayOneShot(otherfx.clip, 1.0f);
  1229.                         gotHitTimer = Time.time + Random.Range(.5f, .75f);
  1230.                     } else {
  1231.                         //Play a small pain sound
  1232.                         otherfx.clip = painLittle;
  1233.                         otherfx.PlayOneShot(otherfx.clip, 1.0f);
  1234.                         gotHitTimer = Time.time + Random.Range(.5f, .75f);
  1235.                     }
  1236.                 }
  1237.             }
  1238.         }else{
  1239.             if (Time.time > gotHitTimer && painDrown) {
  1240.                 //Play a small pain sound
  1241.                 otherfx.clip = painDrown;
  1242.                 otherfx.PlayOneShot(otherfx.clip, 1.0f);
  1243.                 gotHitTimer = Time.time + Random.Range(.5f, .75f);
  1244.             }  
  1245.         }
  1246.        
  1247.         if(!CameraControlComponent.thirdPersonActive){
  1248.             painKickUpAmt = Random.Range(100, -100);//Choose a random view kick up amount
  1249.             if(painKickUpAmt < 50 && painKickUpAmt > 0){painKickUpAmt = 50;}//Maintain some randomness of the values, but don't make it too small
  1250.             if(painKickUpAmt < 0 && painKickUpAmt > -50){painKickUpAmt = -50;}
  1251.            
  1252.             painKickSideAmt = Random.Range(100, -100);//Choose a random view kick side amount
  1253.             if(painKickSideAmt < 50 && painKickSideAmt > 0){painKickSideAmt = 50;}
  1254.             if(painKickSideAmt < 0 && painKickSideAmt > -50){painKickSideAmt = -50;}
  1255.            
  1256.             //create a rotation quaternion with random pain kick values
  1257.             painKickRotation = Quaternion.Euler(mainCamTransform.localRotation.eulerAngles - new Vector3(painKickUpAmt, painKickSideAmt, 0));
  1258.            
  1259.             //make screen kick amount based on the damage amount recieved
  1260.             if(zoomed && !WeaponBehaviorComponent.zoomIsBlock){
  1261.                 appliedPainKickAmt = (damage / (painScreenKickAmt * 10)) / 3;  
  1262.             }else{
  1263.                 appliedPainKickAmt = (damage / (painScreenKickAmt * 10));          
  1264.             }
  1265.            
  1266.             if(blockState){
  1267.                 appliedPainKickAmt = 0.025f; //0.025f
  1268.             }
  1269.            
  1270.             //make sure screen kick is not so large that view rotates past arm models
  1271.             appliedPainKickAmt = Mathf.Clamp(appliedPainKickAmt, 0.0f, 0.15f);  //0.0f, 0.15f
  1272.            
  1273.             //smooth current camera angles to pain kick angles using Slerp
  1274.             mainCamTransform.localRotation = Quaternion.Slerp(mainCamTransform.localRotation, painKickRotation, appliedPainKickAmt );
  1275.         }
  1276.        
  1277.         if(WeaponBehaviorComponent.zoomIsBlock){
  1278.             if(!WeaponBehaviorComponent.hitCancelsBlock){
  1279.                 blockState = false;
  1280.             }else{
  1281.                 zoomed = false;
  1282.             }
  1283.         }
  1284.    
  1285.         //Call Die function if player's hitpoints have been depleted
  1286.         if (hitPoints < 1.0f){
  1287.             SendMessage("Die");//use SendMessage() to allow other script components on this object to detect player death
  1288.         }
  1289.     }
  1290.    
  1291.     void Die () {
  1292.        
  1293.         bulletTimeActive = false;//set bulletTimeActive to false so fadeout wont take longer if bullet time is active
  1294.        
  1295.         if(!FPSWalkerComponent.drowning){
  1296.             //play normal player death sound effect if the player is on land
  1297.             otherfx.clip = die;
  1298.             otherfx.PlayOneShot(otherfx.clip, 1.0f);
  1299.  
  1300.         }else{
  1301.             //play drowning sound effect if the player is underwater    
  1302.             otherfx.clip = dieDrown;
  1303.             otherfx.PlayOneShot(otherfx.clip, 1.0f);
  1304.         }
  1305.        
  1306.         //disable player control and sprinting on death
  1307.         FPSWalkerComponent.inputX = 0;
  1308.         FPSWalkerComponent.inputY = 0;
  1309.         FPSWalkerComponent.cancelSprint = true;
  1310.            
  1311.         GameObject llf = Instantiate(levelLoadFadeObj) as GameObject;//Create instance of levelLoadFadeObj
  1312.         //call FadeAndLoadLevel function with fadein argument set to false
  1313.         //in levelLoadFadeObj to restart level and fade screen out from black on level load
  1314.         llf.GetComponent<LevelLoadFade>().FadeAndLoadLevel(Color.black, 2.0f, false);
  1315.        
  1316.     }
  1317.    
  1318.     public void RestartMap () {
  1319.         Time.timeScale = 1.0f;//set timescale to 1.0f so fadeout wont take longer if bullet time is active
  1320.         GameObject llf = Instantiate(levelLoadFadeObj) as GameObject;//Create instance of levelLoadFadeObj
  1321.         //call FadeAndLoadLevel function with fadein argument set to false
  1322.         //in levelLoadFadeObj to restart level and fade screen out from black on level load
  1323.         llf.GetComponent<LevelLoadFade>().FadeAndLoadLevel(Color.black, 2.0f, false);
  1324.         //set restarting var to true to be accessed by FPSRigidBodyWalker script to stop rigidbody movement
  1325.         restarting = true;
  1326.         // Disable all scripts to deactivate player control upon player death
  1327.         FPSWalkerComponent.inputX = 0;
  1328.         FPSWalkerComponent.inputY = 0;
  1329.         FPSWalkerComponent.cancelSprint = true;
  1330.         WeaponBehaviorComponent.shooting = false;
  1331.     }
  1332.  
  1333. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement