Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
Question by Griffo · May 15, 2013 at 06:47 AM · coroutineupdatefunctionyield

Im a bit confused on a simple script

Hi,

We'll I thought it would be simple.

I'm trying to get this blood fade script to work but getting a little confused.

What I'm doing is calling the damage function from the enemy scripts, so when the enemy is shooting the player the blood fades in, then when they are not the update function fades the blood back out, I did have a global variable controlling the fade out in the update function set to false when the enemy ray cast was hitting the player but when two or more enemy was in the scene it was always set to false if one of them was not attacking the player, so I want it all to be set in this script.

So I'm trying to hold back the fade out while the damage function is being called but getting confused how the best way of doing this would be.

 #pragma strict
 
 var bloodSplatter : Texture2D;
 
 private var fade : float;
 private var gapSet : boolean;
 
 function Update(){
 // If the Player is NOT taking damage slowly fade out the blood effect
     if(!gapSet && fade > 0){
         fade = fade - 0.02;
         GapSetBloodSplatter();
     }
 }
 
 // Function called from enemy scrips
 function damage(healthToDeduct : float){
 // If the Player IS taking damage slowly fade in the blood effect
     if(fade < 1){
         fade = fade + healthToDeduct / 100;
     }
 }
 
 function GapSetBloodSplatter(){
     gapSet = true;
     yield WaitForSeconds (0.05);
     gapSet = false;
 }
 
 function OnGUI(){
     GUI.color.a = fade;
     GUI.DrawTexture(Rect(0, 0, Screen.width, Screen.height), bloodSplatter);
 }
Comment
Add comment · Show 3
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 whydoidoit · May 15, 2013 at 07:39 AM 0
Share

What effect are you currently seeing? I can think of other ways, but I'm not sure that they are any better than this.

avatar image KiraSensei · May 15, 2013 at 07:45 AM 0
Share

So what is happening with this script attached to your player ?

avatar image Griffo · May 15, 2013 at 08:31 AM 0
Share

It's just not fading in the affect smoothly as the update is slowing it down because it's trying to fade it back out while the damage function is trying to fade it in, getting mixed results with different enemy, as they deduct health at different rates, terrorist, droid, mech .. ect. So it's not fading in full when the players health is down to 0.

As I said I did try using a global variable in the enemy scripts to stop the fade out in update if the enemy was locked on and shooting the player, but if other enemy was in the area they would set the variable to false.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by SubatomicHero · May 15, 2013 at 11:37 AM

Ok so line by line lets go through this code:

  • gapSet not being initialised will default to FALSE (hopefully you realise this).

  • I can't see any initialisation of fade to any value and considering its private, it must happen somewhere in the script you've provided but it doesn't (Or maybe it does and you havent provided it).

  • In your update function I think you should wait for fade to be equal to zero before calling GapSetBloodSplatter() (Also make sure it doesnt fall below 0 either).

  • In your GapSetBloodSpatter function maybe it would be good to reset your value of fade to it's default also.

These are just a few observations that may trigger some ideas.

Comment
Add comment · Show 4 · 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 Griffo · May 15, 2013 at 12:10 PM 0
Share

Thanks for the input, gapSet defaults to false in the setting of the variable, then it allows the fading out if fade is > 0 in function update, then it is then set to true in GapSetBloodSplatter no longer allowing the fade until it is reset to false again after 0.05 second, allowing a smooth, not to fast fade of the texture.

Again fade defaults to 0 in the setting of the variable, and never goes below -0.01 or round about because of fade = fade - and fade = fade + being inside if(fade > 0) and if(fade < 0)

The script works but as I read it the if part in function update will slow down the damage function because it's in update and always being called, I know this happens because if I remark it out in update the damage function works more smoothly, so I was just wondering if there was a way to stop the if part in function update while the damage function is being called (from the scrips attached to the enemy)

avatar image KiraSensei · May 15, 2013 at 12:13 PM 0
Share

Don't you want your player to get more damages while he takes some damages from another enemy ?

avatar image SubatomicHero · May 15, 2013 at 12:14 PM 0
Share

maybe setting a boolean to true in your damage function and yielding a second or two to allow the damage function to run, then setting that boolean back to false. That boolean can then be checked in your update function.

avatar image Griffo · May 15, 2013 at 12:35 PM 0
Share

$$anonymous$$iraSensei .. That is happening because each enemy when shooting the player calls -

 playerDamage.GetComponent(PlayerDamage).damage(deductPlayerHealth);

so if 1 or more enemy is shooting the player that gets called from each script attached to each enemy, so deductPlayerHealth set in the inspector depending on what enemy strength will be deducted multiple time so the player will be getting more damage the more enemy attacking.

SubatomicHero .. I've tried that but it stops it running smoothly.

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.

Follow this Question

Answers Answers and Comments

16 People are following this question.

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

Related Questions

Invoke repeating OR coroutine with WaitForSeconds? 1 Answer

How to properly use Yield for a coroutine? 3 Answers

Is there a yield WaitForSeconds type code for the update? 2 Answers

How to create a delay in a function being called in update? 2 Answers

Coroutoutine doesnt work properly. 3 Answers


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