• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by sub0 · Nov 27, 2011 at 08:47 PM · damagebloodcodsplatterhealth

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
6
Best Answer

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Scripter05 · Jan 11, 2012 at 12:23 PM 0
Share

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!

avatar image aldonaletto · Jan 11, 2012 at 12:29 PM 0
Share

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);
  ...
avatar image Scripter05 · Jan 11, 2012 at 12:48 PM 0
Share

$$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!

avatar image aldonaletto · Jan 11, 2012 at 09:11 PM 0
Share

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;
}
avatar image Scripter05 · Jan 12, 2012 at 06:00 AM 0
Share

Thanks a lot! It works as smoothly as clockwork.=)

avatar image
1

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sub0 · Nov 28, 2011 at 02:27 PM 0
Share

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?

avatar image aldonaletto · Jan 11, 2012 at 12:35 PM 1
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Tunnel Vision Health System 3 Answers

how do i make a script that calculate how much damage did i loss 1 Answer

For loop on collision (two hit kill) 2 Answers

Fire Damage 2.0 1 Answer

Damage not triggering in build 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges