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

Water Damage Script

Discussion in 'Scripting' started by DarkWolfSoftware, Jun 24, 2015.

  1. DarkWolfSoftware

    DarkWolfSoftware

    Joined:
    Jun 4, 2015
    Posts:
    31
    I am kind of struggling with this one. What I would like to do is have it so if the player touches the water (runs through it or jumps in it etc.) it causes damage to their health and if they stay in it too long there health will be reduced to zero and they will die. I only want it for a specific race type though. Does anyone know how I might go about setting something like that up using C# or would a Js be better for that?
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    language doesn't really matter... do you have any code already?
     
  3. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,384
    Post some code, and be more specific about what the hangup is.. Kind of hard to believe you have characters and multiple races but can't figure out DoT water.
     
    Kiwasi likes this.
  4. DarkWolfSoftware

    DarkWolfSoftware

    Joined:
    Jun 4, 2015
    Posts:
    31
    That was kind of rude bro, was it really necessary?
     
  5. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,384
    Make a trigger and tag it water, anything in this water with the tag race xyz has an enumerator run which sends damage to the character every x seconds until he leaves the trigger or dies.

    Your question has too many holes in it to answer more thoroughly without making some big assumptions. It's also trivial do do this and considering that you have races, characters, etc already setup it makes one wonder if you're getting ahead of yourself or if you really have a different question entirely but are unaware of it. Like, "how do Triggers work?", or "how do I do damage over time?", these are more appropriate questions.

    If you explained the problem with a code example or something more specific then it would be easier to address the actual problem. For all we know you don't have any code, which makes it pointless to bother answering you with a detailed response because you should be reviewing the basics of C# and Unity beginner tutorials instead.

    Hope that clears things up.
     
  6. DarkWolfSoftware

    DarkWolfSoftware

    Joined:
    Jun 4, 2015
    Posts:
    31
    It was actually a generalized question. I didn't see the point of posting my entire player setup script as it consists of base, character race, character class, character gender etc. I was just asking the easiest method. I mean if you really need all of that here is the base player I have set up.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class BasePlayer  {
    6.  
    7.     private string playerName;
    8.     private int playerLevel;
    9.     private BaseCharacterClass playerClass;
    10.     private int health;     //health
    11.     private int energy;     //energy for abilities
    12.     private int strength;   //physical damage
    13.     private int intellect;  //magical damage
    14.     private int agility;    //attack speed, movement and critical attack
    15.     private int resistance; //defense and resistance to phys/magic damage
    16.  
    17.     private int gold;       //in game currency
    18.  
    19.     private int currentXP;
    20.     private int requiredXP;
    21.     private int statPointsToAllocate;
    22.  
    23.     //public string PlayerName{
    24.         //get{ return playerName;}
    25.         //set{ playerName = value;}
    26.         //}
    27.     public string PlayerName{get; set;} //Short hand method <-
    28.     public int CurrentXP { get; set; }
    29.     public int RequiredXP { get; set; }
    30.     public int StatPointsToAllocate { get; set; }
    31.  
    32.     public int PlayerLevel{
    33.         get{ return playerLevel;}
    34.         set{ playerLevel = value;}
    35.     }
    36.     public BaseCharacterClass PlayerClass{
    37.         get{ return playerClass;}
    38.         set{ playerClass = value;}
    39.     }
    40.     public int Health{
    41.         get{ return health;}
    42.         set{ health = value;}
    43.     }
    44.     public int Energy{
    45.         get{ return energy;}
    46.         set{ energy = value;}
    47.     }
    48.     public int Strength{
    49.         get{ return strength;}
    50.         set{ strength = value;}
    51.     }
    52.     public int Intellect {
    53.         get{ return intellect;}
    54.         set{ intellect = value;}
    55.  
    56.         }
    57.  
    58.     public int Agility {
    59.         get{ return agility;}
    60.         set{ agility = value;}
    61.     }
    62.  
    63.     public int Resistance {
    64.         get{ return resistance;}
    65.         set{ resistance = value;}
    66.     }
    67.  
    68.     public int Gold {
    69.         get{ return gold;}
    70.         set{ gold = value;}
    71.     }
    72.  
    73.  
    74.  
    75.  
    76.  
    77.  
    78.  
    79.  
    80. }
    81.  
    What is the point of wandering through all of that when the simple answer was

    "Hey, just set up a trigger!"
    or
    "Hey, look up triggers in the unity documentation!"

    Instead of insulting my intelligence or calling me a beginner who doesn't have any code and randomly asking questions.

    Their is really no need to be an ass and throw out insults. You can throw reasoning into it all you want but there is never a good excuse to be rude to someone when it isn't necessary.

    Thanks for the info though.
     
  7. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,384
    I didn't insult. I simply found it very strange that you were asking something like that and implying that you we're actually stuck and could not proceed, with no evidence of why. Seems sensible to assume you didn't try, give the circumstances.

    I didn't need the entire player script, we needed to know what you are stuck on in regards to your script which was not posted.. After all, this is the Scripting section.

    No need to get all upset about it.
     
  8. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    I'd recommend you try some of the tutorial projects in the learn section, your code doesn't do anything but set up some attributes (and you've lumped everything into a single megascript which isn't generally a good idea).

    If you follow the basic tutorials they'll show you how to build components to handle health/damage dealing etc.

    http://unity3d.com/learn/tutorials/modules
     
  9. DarkWolfSoftware

    DarkWolfSoftware

    Joined:
    Jun 4, 2015
    Posts:
    31
    Oh good lord, its a base player script and simply an example, secondly if I knew i was just going to get "Go do tutorials newb!" replies I wouldn't have bothered. I will just figure it out for myself. Lol, this why I don't normally ask questions on forums. Way too many ego responses.
     
    nicshaw14 likes this.
  10. Korno

    Korno

    Joined:
    Oct 26, 2014
    Posts:
    518
    I think it is more that there are loads of "I NEED X. GIF SCRIPTZ THAT DO X PLZ" threads from people who want everything done for them while not being able to do anything themselves. It does annoy people. I don't think your question was like that (some of the comments weren't really called for to be honest) but people generally want to see that an effort has been made first before helping.

    That said, about your question - I would have the water contained by a box collider set to trigger. When the characters are in the water (OnTriggerStay will be called every frame that a collider is in a trigger - very useful for this) you could apply a damage to the character. If you defined the damage as Damage per second you could minus the damage by:

    health -= damagePerSecond * Time.deltaTime;

    As for the race type, at the start of the race, I would check the race type and disable/enable the trigger based on this. That way the code will only be executed in that race type (no needless if checks every frame).

    Hope that my, badly spelt and written, braindump helps...
     
    DarkWolfSoftware likes this.
  11. DarkWolfSoftware

    DarkWolfSoftware

    Joined:
    Jun 4, 2015
    Posts:
    31
    Thank you!! Thats all I needed to know. +1!