Last active
August 28, 2019 10:06
-
-
Save IRobS/fa483af1c0a5f3dd1662ce9e659c1093 to your computer and use it in GitHub Desktop.
the player life - handles health, death, ragdoll
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* the player life - handles health, death, ragdoll | |
*/ | |
background: #d99; | |
min-height: 100%; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<pre> | |
<code> | |
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.UI; | |
using UnityStandardAssets.Characters; | |
using UnityStandardAssets.Characters.FirstPerson; | |
public class PlayerLife : MonoBehaviour | |
{ | |
public RawImage[] healthImages; | |
public Image bloodImage; | |
private int currentHealth; | |
private bool damageImmune = false; | |
private bool showDamage = false; | |
public Transform trans; | |
public Vector3 respawnPosition; | |
private Overseer theGreatOverseer; | |
// Start is called before the first frame update | |
void Start() | |
{ | |
currentHealth = 3; | |
respawnPosition = transform.position; | |
theGreatOverseer = FindObjectOfType<Overseer>(); | |
} | |
// Update is called once per frame | |
void Update() | |
{ | |
//update drawing our hearts | |
for (int i = 0; i < healthImages.Length; i++) | |
{ | |
if (i < currentHealth) | |
{ | |
healthImages[i].enabled = true; | |
} | |
else | |
{ | |
healthImages[i].enabled = false; | |
} | |
} | |
//update drawing our damage indicator (the bloodimage) | |
if (showDamage) | |
{ | |
Color Opaque = new Color(1, 0.1f, 0.1f, 1); | |
bloodImage.color = Color.Lerp(bloodImage.color, Opaque, 20 * Time.deltaTime); | |
if (bloodImage.color.a >= 0.8) //Almost Opaque, close enough | |
{ | |
showDamage = false; | |
} | |
} | |
if (!showDamage) | |
{ | |
Color Transparent = new Color(1, 0.1f, 0.1f, 0); | |
bloodImage.color = Color.Lerp(bloodImage.color, Transparent, 8 * Time.deltaTime); | |
} | |
} | |
//called by other game entities to damage the player | |
public void takeDamage() | |
{ | |
if (!damageImmune) | |
{ | |
currentHealth--; | |
damageImmune = true; | |
showDamage = true; | |
StartCoroutine(endImmunity(2)); | |
} | |
if (currentHealth <= 0) | |
{ | |
death(); | |
} | |
} | |
//ends the damage imunity after a time delay | |
IEnumerator endImmunity(float time) | |
{ | |
yield return new WaitForSeconds(time); | |
damageImmune = false; | |
} | |
void death() | |
{ | |
//create our ragdoll | |
Transform death = Instantiate(trans, transform.position, transform.rotation); | |
CopyTransformsRecurse(transform, death); | |
//tell the overseer to respawn us | |
theGreatOverseer.respawnPlayer(this, 3f); | |
//die | |
gameObject.SetActive(false); | |
} | |
//creating the ragdoll | |
static void CopyTransformsRecurse(Transform src, Transform dst) | |
{ | |
dst.position = src.position; | |
dst.rotation = src.rotation; | |
dst.gameObject.active = src.gameObject.active; | |
foreach (Transform child in dst) | |
{ | |
// match the transform with the same name | |
var curSrc = src.Find(child.name); | |
if (curSrc) CopyTransformsRecurse(curSrc, child); | |
} | |
} | |
public void respawn() | |
{ | |
transform.position = respawnPosition; | |
gameObject.SetActive(true); | |
currentHealth = 3; | |
damageImmune = false; | |
} | |
} | |
</code> | |
</pre> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// alert('Hello world!'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{"view":"split","fontsize":"100","seethrough":"","prefixfree":"1","page":"html"} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment