- Home /
blood damage like call of duty?
Hi guys,
Is it possible in Unity to make the health as blood damage like in call of duty?
I mean like this image:
http://img.gamespot.com/gamespot/images/2009/315/reviews/951942_20091112_embed004.jpg
Thanks in advance
Answer by aldonaletto · Nov 28, 2011 at 05:29 AM
You should use one or more GUITexture, and control its color.a property according to the player's health, like @anwe said. It's better to use two or three GUITexture, each one with a different blood splash pattern. Each GUITexture must start appearing at a different health level, thus the screen will become more and more bloody while the player life is going away.
You could do the following: create the blood images with transparent background, and import them using Assets/Import New Asset; select each image in Project and click GameObject/Create Other/GUITexture - this will create a GUITexture with the selected image; drag the GUITextures created to each bloodGuiN variable in the script below:
var bloodGui1: GUITexture; // drag here the GUITextures you've created:
var bloodGui2: GUITexture; // bloodGui1 should have the lower blood density,
var bloodGui3: GUITexture; // and bloodGui3 the higher density.
function Start(){
// set GUITexture.pixelInset to the whole screen:
var r = Rect(-Screen.width/2, -Screen.height/2, Screen.width, Screen.height);
bloodGUI1.pixelInset = r;
bloodGUI2.pixelInset = r;
bloodGUI3.pixelInset = r;
}
// Returns the alpha value proportional to the health loss;
// "start" tells the health percentage below which the blood starts appearing
function BloodAlpha(start: float): float {
var alpha = 1 - (100 * health)/(start * normalHealth);
return Mathf.Clamp(alpha, 0, 1);
}
function Update(){
bloodGUI1.color.a = BloodAlpha(100); // start appearing when health < 100%
bloodGUI2.color.a = BloodAlpha(60); // start appearing when health < 60%
bloodGUI3.color.a = BloodAlpha(30); // start appearing when health < 30%
}
NOTE: this script assumes that health contains the current player health, and normalHealth is the max health under normal conditions (no extra health). Change these names to the ones you are actually using. If both are static variables, you can use this code in a separate script; if not, add this code to your health script or wherever these variables are declared.
Hi aldonaletto. I'm trying to understand your script. I almost got it, but not all. How will this code look in c#? I confused with "function BloodAlpha(start: float): float" $$anonymous$$y supposition is: void BloodAlpha(float start) { float alpha = 1 - (100 health)/(start normalHealth); return $$anonymous$$athf.Clamp(alpha, 0, 1); } Is this right? Thanks!
Almost right... this function in C# is float BloodAlpha(...), like below:
float BloodAlpha(float start){ float alpha = 1 - (100 * health)/(start * normalHealth); return $$anonymous$$athf.Clamp(alpha, 0, 1); }And Start becomes:
void Start(){ // set GUITexture.pixelInset to the whole screen: Rect r = new Rect(-Screen.width/2, -Screen.height/2, Screen.width, Screen.height); ...
$$anonymous$$any thanks! I've got an error: error CS1612: Cannot modify a value type return value of `UnityEngine.GUITexture.color'. Consider storing the value in a temporary variable. I think there is a mistake in your script . bloodGUI1.color in the end does not match with bloodGui1 in the start... or am I wrong? Thanks!
This works fine in javascript, but C# doesn't allow changing a single component of a property. Like the error message said, you must use a temporary variable like this:
void Update(){ Color temp; // temporary variable temp = bloodGUI1.color; temp.a = BloodAlpha(100); // start appearing when health < 100% bloodGUI1.color = temp; temp = bloodGUI2.color; temp.a = BloodAlpha(60); // start appearing when health < 60% bloodGUI2.color = temp; temp = bloodGUI3.color; temp.a = BloodAlpha(30); // start appearing when health < 30% bloodGUI3.color = temp; }
Answer by anwe · Nov 27, 2011 at 08:57 PM
of course, just try to put a texture or a plane over the camera and modify its alpha component of color, so that u can't see it if u have got 100 health, but if u've got jut 10 u'll see the blood damage
What I understood from you guys is that the health is decreasing after taking damage.But I want the health to refill after taking damage and after few seconds of the appearance of blood, exactly like in COD. Can the code you wrote (aldonaletto) be applied in this situation?
This is done in the health script - you must increment health over time with something like this:
var healSpeed: float = 10;
And in Update:
if (health < maxHealth) health += healSpeed * Time.deltaTime;
NOTE: health must be a float; if it's an int, it will never be incremented
Your answer
![](https://koobas.hobune.stream/wayback/20220613055237im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Tunnel Vision Health System 3 Answers
Health Regeneration 2 Answers
Health Bar fire damage 1 Answer
Universal Damage Sytem 2 Answers
Enemy Health, Player Damage 0 Answers