Skip to content

Instantly share code, notes, and snippets.

@IRobS
Last active August 28, 2019 10:06
Show Gist options
  • Save IRobS/fa483af1c0a5f3dd1662ce9e659c1093 to your computer and use it in GitHub Desktop.
Save IRobS/fa483af1c0a5f3dd1662ce9e659c1093 to your computer and use it in GitHub Desktop.
the player life - handles health, death, ragdoll
/**
* the player life - handles health, death, ragdoll
*/
background: #d99;
min-height: 100%;
<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&ltOverseer&gt();
}
// Update is called once per frame
void Update()
{
//update drawing our hearts
for (int i = 0; i &lt healthImages.Length; i++)
{
if (i &lt 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 &gt= 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 &lt= 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>
// alert('Hello world!');
{"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