I was wondering how I can make an enemy flash a grey or white colour when a bullet hits them? I have a health script called ‘EnemyHealth’ and a bullet script which interact with one another.
Here is my bullet script:
public class PlayerShooting : MonoBehaviour
{
//Weapon Collider Script
void OnTriggerEnter(Collider hit)
{
if (hit.gameObject.tag == "Enemy")
{
hit.GetComponent<EnemyHealth>().TakeDamage (10);
Destroy(gameObject);
return;
}
}
There’s a few ways you could go about this, but the easiest would be to go through your characters shader. If you were to add a property that changes the diffuse color, or maybe emission, then you could ‘animate’ that value from code when you take damage and make your character flash
Yeah a few people around the web have suggested this method to other people, but I couldn’t get any of them to work for me. I’ve only been using Unity for just over a week so I’m not very familiar with many of the commands and how they work.
Yes im just using the standard shader. Some of my enemies have multiple parts to their bodies, each with their own shader. Is this going to complicate things?
Play around with it in the inspector to see what it does to your model. Once you have a colour that you would like for your flash effect, you can start coding it.
OK thanks for that, I’ll take a look at that link and have a play with it. I assume the code I use needs to go in the same script that’s attached to the bullet?
Depends on how you want to do it really. I would add it to your character and enemy classes myself (bonus points if they both inherit from the same base class that manages health)
That way you can destroy your bullets the second they impact and still run your code that makes the characters flash
OK I added the code to the health script, but in the console it says error 'There is no ‘Renderer’ attached to the “UFO” game object, but a script is trying to access it.
The renderer is not part of the main body, the renderer is attach to a part of the enemy, as you can see here:
I changed GetComponent to GetComponentInChildren and with one of my enemies this now changes the renderer to a solid colour, but it stays that colour, it doesnt change back to normal between shots. Here is the exact script I now have:
public void TakeDamage(int amount)
{
// If the enemy is dead...
if (isDead)
return;
// Reduce the current health by the amount of damage sustained.
currentHealth -= amount;
if (currentHealth > 0)
{
Renderer renderer = GetComponentInChildren<Renderer>();
Material mat = renderer.material;
float emission = Mathf.PingPong(Time.time, 1.0f);
Color baseColor = Color.yellow; //Replace this with whatever you want for your base color at emission level '1'
Color finalColor = baseColor * Mathf.LinearToGammaSpace(emission);
mat.SetColor("_EmissionColor", finalColor);
}
// If the current health is less than or equal to zero...
if (currentHealth <= 0)
{
// ... the enemy is dead.
Death();
}
}
This code still wont work for my ‘UFO’ enemy though.
The problem is I can’t use Coroutines or invokes or anything like that because of the way my script is set up. This is the section that links to the bullet script:
public void TakeDamage(int amount)
{
// If the enemy is dead...
if (isDead)
// ... no need to take damage so exit the function.
return;
// Reduce the current health by the amount of damage sustained.
currentHealth -= amount;
if (currentHealth > 0)
{
//code needs to go here
}
// If the current health is less than or equal to zero...
if (currentHealth <= 0)
{
// ... the enemy is dead.
Death();
}
}
So the code needs to go here part of the script tells the game if the enemy takes damaged and if(currentHealth > 0) then do something. All I have had success with so far is using Instantiate to create a small particle effect to signify the enemy is being hit.
This actually works OK for regular enemies, but if I have a larger boss enemy its not so convincing, plus when he is finally destroyed he will need to flash numerous times before disappearing in a fireball.