Skip to content

Instantly share code, notes, and snippets.

@jcordeiro67
Last active February 10, 2017 19:30
Show Gist options
  • Select an option

  • Save jcordeiro67/13b775641453d1aaaeadb5f3c8a78b1b to your computer and use it in GitHub Desktop.

Select an option

Save jcordeiro67/13b775641453d1aaaeadb5f3c8a78b1b to your computer and use it in GitHub Desktop.
My Defender Script / Used to change the attackers color of the defender when attacked
/* DESCRIPTION:
* Changes the color of the sprite when hit
* by a Projectile then back to it's original
* color over a float time in the IEnumerator's
* waitforSeconds.
*
* REQUIRED:
* Requires a Sprite Renderer component on the
* child gameObject of the defender.
*
* USAGE:
* To use in Attackers.cs simply change the <Attacker> component
* to <Projectile> or what ever you named your projectile script.
*/
using UnityEngine;
using System.Collections;
public class Defender : MonoBehaviour {
// Used currently as a class tag for GetComponent only
[Tooltip ("Swap Delay: Float The length of time to swap back to the " +
"sprites original color")]
public float swapDelay = 0.05f;
[Tooltip ("Hit Color: RGBA the color the sprite swaps to when it")]
public Color hitColor;
// The SpriteRenderer on the child gameObject
private SpriteRenderer sprite;
void Start(){
sprite = GetComponentInChildren <SpriteRenderer>();
}
void OnTriggerEnter2D(Collider2D col){
// Convert the collider2D to a gameObject
GameObject obj = col.gameObject;
// Leave the method if not a Attacker
if(!obj.GetComponent <Attacker>()){
return;
}
// If spriteRenderer found
if(sprite != null){
StartCoroutine (FlashHitColor ());
}
}
IEnumerator FlashHitColor(){
// Changes the color when hit by a projectile
// then back over time(swapDelay)
sprite.color = hitColor;
yield return new WaitForSeconds (swapDelay);
sprite.color = Color.white;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment