Question by
Emiven · Nov 21, 2016 at 05:58 PM ·
collision detectioncolor changelerpingfading problem
How do I make tiles that light up when gameobject touches them?
Hello! I'm making a 2D twinstick shooter, and i wanted a interesting background of tiles that light up and fades away when player and enemies move over them. I have two problems:
1: the change of colour and fade back seems only to happen the first time i runb over them with the gameobject i am using Mathf.SmoothStep to achieve the fading btw.
2: when i group the tiles together, it seems to have a big impact on performance. It laggs like hell when i drag the board of tiles around, and sometimes even unitu freezes up. The tiles are made up of a sprite render and a box collider that is a trigger.
Here is the code for the tiles:
public class glow : MonoBehaviour {
private SpriteRenderer s;
public bool fading = false;
private float startTime;
private float t;
float fadeTime = 4.0f;
void Start ()
{
s = GetComponent<SpriteRenderer> ();
startTime = Time.time;
s.color = Color.black;
}
IEnumerator flash(float time)
{
yield return new WaitForSeconds (time);
}
void colourChange()
{
s.color = new Color (60.0f/255.0f, 200.0f/255.0f,200.0f/255.0f, 1.0f);
}
void colourFade()
{
t = (Time.time - startTime) / fadeTime;
s.color = new Color (Mathf.SmoothStep (60.0f/255.0f, 0f, t), Mathf.SmoothStep (200.0f/255.0f, 0f, t), Mathf.SmoothStep (200.0f/255.0f, 0f, t), 1.0f);
}
void OnTriggerEnter2D(Collider2D other)
{
if(other.gameObject.tag == "Player")
{
colourChange ();
}
}
void OnTriggerExit2D(Collider2D other)
{
if(other.gameObject.tag == "Player")
{
fading = true;
StartCoroutine(flash(4.0f));
}
}
void Update ()
{
if(fading == true)
{
colourFade ();
}
}
}
Thank you :)
Comment