Hi
I have been trying to create a blood pool everytime I kill a victim
It turned out to be a bit more difficult than expected, and I fear that my end result… although it works, is not the optimal sollution. I would be very gratefull, if someone had suggestions on how to do this in a better way. So, if anyone has offers on how to improve this, please share with me.
using UnityEngine;
using System.Collections;
public class AlphaFader : MonoBehaviour
{
public Texture2D blood;
public Color lerpedColor;
public bool startFading = false;
public float trans = 1;
void Start ()
{
//Sets the material of the gameObject
renderer.material = new Material (Shader.Find ("Transparent/Diffuse"));
renderer.material.SetTexture("_MainTex", blood);
//Starts the corutine
StartCoroutine(StartFading());
}
void Update ()
{
if(startFading){
//Removes some of the trans that is used to lert the alpha of the material.
trans = trans - Time.deltaTime * 0.2f;
lerpedColor.a = Mathf.Lerp(0, 1, trans);
renderer.material.color = lerpedColor;
}
//Destroys the object when the material is completly transparent.
if (lerpedColor.a == 0)
Destroy (this.gameObject);
}
//This will waith for a while before starting the fade of the material.
IEnumerator StartFading (){
yield return new WaitForSeconds(Random.value * 4.0f);
startFading = true;
}
}