Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Help With Booleans and a Void(?)

Discussion in 'Scripting' started by DoubleDonjon, Dec 23, 2015.

  1. DoubleDonjon

    DoubleDonjon

    Joined:
    Feb 10, 2015
    Posts:
    9
    Hi, so i'm making a multiplayer game using Photon Unity Networking and I'm having some issues with a void(function? the thing where you say void blahblahblah, then you can say blahblahblah(some value)) and a boolean(showDeathVignette). Basically, I want the boolean to be set as true when the void/function thingy is called, and when it's not, it's false.

    Code (CSharp):
    1.  
    2. [RPC]
    3. public void TakeDamage(float amt) {
    4.         currentHitPoints -= amt;
    5.  
    6.         if(currentHitPoints <= 0) {
    7.             Die();
    8.         }
    9.         showDeathVignette = true;
    10.     }
    11.  
    12. void OnGUI() {
    13.         if (showDeathVignette) {
    14.             GUI.DrawTexture(new Rect (0, 0, Screen.width, Screen.height), dVT);
    15.         }
    16.    }
    17.     void Update() {
    18.         if( this.transform.position.y <= new Vector3(0, -20, 0).y ) {
    19.             TakeDamage(10000);
    20.         }
    21.         showDeathVignette = false;
    22.     }
    23.    
    This is my code so far, and I see the problem with setting showDeathVignette to false in the update, but I don't see a another way that I could try to achieve that.
     
  2. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Shouldn't you only set it to true when the player dies? IE - their hit points drop to 0. In that case, shouldn't you set it to true in that check in TakeDamage - or more appropriately, in Die()? Also, why is damage amount a float? Can a player take decimal damage?
     
  3. DoubleDonjon

    DoubleDonjon

    Joined:
    Feb 10, 2015
    Posts:
    9
    The weapon in the game is a laser, so I wanted to make it so your screen has this red vignette effect when your being hit, so you know that your being hit. I guess it's misleading that it's call "death" vignette, sorry about that. And I think your right about letting the player take decimal damage but the problem is that there only one gun in the game so far and it has a set damage, but I see what the benefit would be.
     
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,848
    If I understand you correctly, the problem is that you want to show a vignette (overlay) for a little while (maybe half a second or whatever), and you don't know how to do that.

    I would do it by, first, ditching OnGUI, and using the new UI system. Add a Canvas to the scene, and put your vignette inside that. Set its alpha to 0, so it's initially invisible. Then when you take damage, just do:
    Code (CSharp):
    1. vignette.color = color.white;
    2. vignette.CrossFadeAlpha(0, 0.5f, false);
    This will set the alpha to 1 and then fade it back to 0 over 0.5 seconds. Easy peasy. (Here vignette would be a property of type Graphic, which you'd hook up as a reference to the vignette in your Canvas.)
     
    KelsoMRK likes this.
  5. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    My point was that if hit points is an int then subtracting a float from it is the same as subtracting an int; so might as well make it an int. I've also never see a game where you could have 15.367 HP :)

    To your issue: you need to turn the effect off after a certain amount of time. The easiest way to do this is to log the time when you turned it on and then check it. So in your TakeDamage method after you set it to true you can do this
    Code (csharp):
    1.  
    2. timeHit = Time.time;
    3.  
    And, of course, add a timeHit float to your class

    Then in your Update, instead of just turning it to false you evaluate the elapsed time since the player got hit last
    Code (csharp):
    1.  
    2. if (showDeathVignette && Time.time - timeHit > 5)
    3. {
    4.     showDeathVignette = false;
    5. }
    6.  
    This will make the effect appear for 5 seconds after the player is hit.
     
  6. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    I like this. Does it use a coroutine internally? If the player is hit in rapid succession does it cancel any existing effects?
     
  7. DoubleDonjon

    DoubleDonjon

    Joined:
    Feb 10, 2015
    Posts:
    9
    Okay, A questions:
    1. Where do I add the canvas? My game's in 3D and when you join the network it instantiates the player into the world. Would I put the canvas on the camera in the player it instantiates? Sorry, I'm a real newbie in terms of GUI stuff.
     
  8. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,848
    Who knows? It's magic.

    Yes, each new call to CrossFadeAlpha cancels any previous one in progress, or so I believe.

    You don't put a canvas on a camera; you just put it in your scene hierarchy, usually at the root level. (You can have an associated camera in some cases, but you probably don't need to bother with that.)

    Spend an hour with the UI tutorials and you'll know all the basics. But in this case, it really is about as simple as I described above.[/QUOTE][/QUOTE]