Created
January 7, 2021 09:24
-
-
Save Quickz/59cef0e109fbab0d89484afbf0999068 to your computer and use it in GitHub Desktop.
Low health warning effect script example for Unity.
This file contains hidden or 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
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); | |
} | |
} |
This file contains hidden or 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
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