Skip to content

Instantly share code, notes, and snippets.

@Quickz
Created January 7, 2021 09:24
Show Gist options
  • Save Quickz/59cef0e109fbab0d89484afbf0999068 to your computer and use it in GitHub Desktop.
Save Quickz/59cef0e109fbab0d89484afbf0999068 to your computer and use it in GitHub Desktop.
Low health warning effect script example for Unity.
using UnityEngine;
[ExecuteInEditMode]
[RequireComponent(typeof(Camera))]
public class CameraEffect : MonoBehaviour
{
public Material material;
private void OnRenderImage(RenderTexture source, RenderTexture destination)
{
if (material == null)
{
Graphics.Blit(source, destination);
return;
}
Graphics.Blit(source, destination, material);
}
}
using UnityEngine;
public class Health : MonoBehaviour
{
public float CurrentAmount
{
get
{
return _currentAmount;
}
set
{
_currentAmount = value;
UpdateCameraEffectBasedOnHealth();
}
}
private float _currentAmount;
[SerializeField]
private CameraEffect cameraEffect = null;
[SerializeField]
private float maxAmount = 100f;
[SerializeField]
private Material lowHealthEffect = null;
private void Start()
{
CurrentAmount = maxAmount;
}
private void UpdateCameraEffectBasedOnHealth()
{
bool healthIsLessThanHalf = CurrentAmount < maxAmount / 2f;
if (healthIsLessThanHalf)
cameraEffect.material = lowHealthEffect;
else
cameraEffect.material = null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment