//add hitpoints to player health public void HealPlayer(float healAmt) { if (hitPoints < 1.0f) //Don't add health if player is dead { return; } //Update health GUIText HealthText HealthText = healthGuiObjInstance.GetComponent <HealthText>(); HealthText[] HealthText2 = healthGuiObjInstance.GetComponentsInChildren <HealthText>(); //Apply healing if (hitPoints + healAmt > maximumHitPoints) { hitPoints = maximumHitPoints; } else { hitPoints += healAmt; } //set health hud value to hitpoints remaining HealthText.healthGui = Mathf.Round(hitPoints); HealthText2[1].healthGui = Mathf.Round(hitPoints); //change color of hud health element based on hitpoints remaining if (hitPoints <= 25.0f) { HealthText.GetComponent <GUIText>().material.color = Color.red; } else if (hitPoints <= 40.0f) { HealthText.GetComponent <GUIText>().material.color = Color.yellow; } else { HealthText.GetComponent <GUIText>().material.color = HealthText.textColor; } }
//remove hitpoints from player health public void ApplyDamage(float damage) { FPSRigidBodyWalker FPSWalkerComponent = GetComponent <FPSRigidBodyWalker>(); float appliedPainKickAmt; if (hitPoints < 1.0f) //Don't apply damage if player is dead { return; } timeLastDamaged = Time.time; //Update health GUIText HealthText HealthText = healthGuiObjInstance.GetComponent <HealthText>(); HealthText[] HealthText2 = healthGuiObjInstance.GetComponentsInChildren <HealthText>(); Quaternion painKickRotation; //Set up rotation for pain view kicks int painKickUpAmt = 0; int painKickSideAmt = 0; hitPoints -= damage; //Apply damage //set health hud value to hitpoints remaining HealthText.healthGui = Mathf.Round(hitPoints); HealthText2[1].healthGui = Mathf.Round(hitPoints); //change color of hud health element based on hitpoints remaining if (hitPoints <= 25.0f) { HealthText.GetComponent <GUIText>().material.color = Color.red; } else if (hitPoints <= 40.0f) { HealthText.GetComponent <GUIText>().material.color = Color.yellow; } else { HealthText.GetComponent <GUIText>().material.color = HealthText.textColor; } GameObject pf = Instantiate(painFadeObj) as GameObject; //Create Instance of painFadeObj pf.GetComponent <PainFade>().FadeIn(PainColor, 0.75f); //Call FadeIn function in painFadeObj to fade screen red when damage taken if (!FPSWalkerComponent.holdingBreath) { //Play pain sound when getting hit if (Time.time > gotHitTimer && painBig && painLittle) { // Play a big pain sound if (hitPoints < 40.0f || damage > 30.0f) { AudioSource.PlayClipAtPoint(painBig, mainCamTransform.position); gotHitTimer = Time.time + Random.Range(.5f, .75f); } else { //Play a small pain sound AudioSource.PlayClipAtPoint(painLittle, mainCamTransform.position); gotHitTimer = Time.time + Random.Range(.5f, .75f); } } } else { if (Time.time > gotHitTimer && painDrown) { //Play a small pain sound AudioSource.PlayClipAtPoint(painDrown, mainCamTransform.position); gotHitTimer = Time.time + Random.Range(.5f, .75f); } } painKickUpAmt = Random.Range(100, -100); //Choose a random view kick up amount if (painKickUpAmt < 50 && painKickUpAmt > 0) { painKickUpAmt = 50; } //Maintain some randomness of the values, but don't make it too small if (painKickUpAmt < 0 && painKickUpAmt > -50) { painKickUpAmt = -50; } painKickSideAmt = Random.Range(100, -100); //Choose a random view kick side amount if (painKickSideAmt < 50 && painKickSideAmt > 0) { painKickSideAmt = 50; } if (painKickSideAmt < 0 && painKickSideAmt > -50) { painKickSideAmt = -50; } //create a rotation quaternion with random pain kick values painKickRotation = Quaternion.Euler(mainCamTransform.localRotation.eulerAngles - new Vector3(painKickUpAmt, painKickSideAmt, 0)); //make screen kick amount based on the damage amount recieved if (zoomed) { appliedPainKickAmt = (damage / (painScreenKickAmt * 10)) / 3; } else { appliedPainKickAmt = (damage / (painScreenKickAmt * 10)); } //make sure screen kick is not so large that view rotates past arm models appliedPainKickAmt = Mathf.Clamp(appliedPainKickAmt, 0.0f, 0.15f); //smooth current camera angles to pain kick angles using Slerp mainCamTransform.localRotation = Quaternion.Slerp(mainCamTransform.localRotation, painKickRotation, appliedPainKickAmt); //Call Die function if player's hitpoints have been depleted if (hitPoints < 1.0f) { Die(); } }