Search Unity

Casting / health script for RPG game

Discussion in 'Scripting' started by Spaetten1, Aug 14, 2010.

  1. Spaetten1

    Spaetten1

    Joined:
    Mar 23, 2010
    Posts:
    85
    Hey guys, I'm messing with this script and can't get it to work:
    Code (csharp):
    1. // The effect that comes during casting time, not the spell fired
    2. var castParticle : Transform;
    3.  
    4. // Put the name of the specific spell to make it print in the cast bar
    5. var spellName = "";
    6.  
    7. var boxRect = 100;
    8.  
    9.  
    10. static var castMax = 200;
    11.  
    12. private static var castNow : int = 0; // Dont mess with that
    13.  
    14. private var castingSpell = false;
    15. var callCastingMSG = false;
    16.  
    17. function Update(){
    18.     if(Input.GetMouseButtonDown(1)  castingSpell == false){
    19.         castingSpell = true;
    20.     }
    21.    
    22.     else if(Input.GetMouseButtonDown(1)  castingSpell == true){
    23.         callCastingMSG = true;
    24.     }
    25.    
    26.     if(castingSpell == true){
    27.         CastFunction();
    28.     }
    29. }
    30.        
    31. function OnGUI () {
    32.     GUI.Box(Rect (10, 10, boxRect, 30), "Health: " + boxRect);
    33.    
    34.     if(callCastingMSG == true){
    35.         CastingSpellMSG();
    36.     }
    37.    
    38.     if(castingSpell == true){
    39.         MakeGUI();
    40.     }
    41. }
    42.  
    43. function OnMouseEnter(){
    44.     boxRect -= 10;
    45. }
    46.  
    47. function CastFunction(){
    48.     var instantiated = false;
    49.    
    50.     if(instantiated == false){
    51.        
    52.         var instParticle = Instantiate(castParticle, transform.position, transform.rotation);
    53.         instantited = true;
    54.     }
    55.  
    56.     while (castNow<castMax){
    57.         yield WaitForSeconds(0.1);
    58.         castNow++;
    59.     }
    60.    
    61.     if (castNow == castMax){
    62.         castingSpell = false;
    63.         castNow = 0;
    64.        
    65.         if(instantiated == true)
    66.             Destroy(instParticle.gameObject);
    67.     }
    68. }
    69.  
    70. function CastingSpellMSG(){
    71.     GUI.Label(Rect (50, 10, 100, 100), "Already Casting Spell");
    72.     yield WaitForSeconds(1);
    73.     callCastingMSG = false;
    74. }
    75.  
    76. function MakeGUI(){
    77.     GUI.Box(Rect (545, 500, 210, 30), "");
    78.     GUI.Button(Rect (545, 500, castNow, 20), "");
    79. }
    The problem is, in the while loop it waits for 0.1 second, and then executes the rest of the code super fast. I want it to wait for 0.1 second every time the castNow variable is less that castMax.

    Plus in the castFunction(), it instantiates "instParticle" like a hundred times and never deletes it - I only want it to be instantiated 1 time and then it should be deleted whenever the casting time is over.

    Hope you can help me out here :)

    *edit* that boxRect is just some health thing I played with :p
     
  2. Spaetten1

    Spaetten1

    Joined:
    Mar 23, 2010
    Posts:
    85
    Can someone please help me? From my point of view it shouldn't be that hard for a real coder :p
     
  3. Ezzerland

    Ezzerland

    Joined:
    Jul 1, 2010
    Posts:
    405
    not 100% sure where you are going with this.

    To fix the destroy bug, you need to fix your spelling error and set instatiated to true (you currently have instatited = true;)

    For the other bit, Are you trying to only instantiate the spell one time, and prevent other spells from being cast while you're doing your casting time, and the destroy the item when casting time is done?

    if so, code below:
    Code (csharp):
    1. // The effect that comes during casting time, not the spell fired
    2. var castParticle : Transform;
    3.  
    4. // Put the name of the specific spell to make it print in the cast bar
    5. var spellName = "";
    6.  
    7. var boxRect = 100;
    8.  
    9.  
    10. static var castMax = 200;
    11.  
    12. //private static var castNow : int = 0; // Dont mess with that
    13.  
    14. private var castingSpell = false;
    15. var callCastingMSG = false;
    16.  
    17. function Update(){
    18.     if(Input.GetMouseButtonDown(1)  castingSpell == false){
    19.         castingSpell = true;
    20.     }
    21.    
    22.     else if(Input.GetMouseButtonDown(1)  castingSpell == true){
    23.         callCastingMSG = true;
    24.     }
    25.    
    26.     if(castingSpell == true){
    27.         CastFunction();
    28.     }
    29. }
    30.        
    31. function OnGUI () {
    32.     GUI.Box(Rect (10, 10, boxRect, 30), "Health: " + boxRect);
    33.    
    34.     if(callCastingMSG == true){
    35.         CastingSpellMSG();
    36.     }
    37.    
    38.     if(castingSpell == true){
    39.         MakeGUI();
    40.     }
    41. }
    42.  
    43. function OnMouseEnter(){
    44.     boxRect -= 10;
    45. }
    46.  
    47. function CastFunction(){
    48.     var instantiated = false;
    49.    
    50.     if(instantiated == false){
    51.        
    52.         var instParticle = Instantiate(castParticle, transform.position, transform.rotation);
    53.         instantiated = true;
    54.     }
    55.  
    56.     for (castNow : int = 0; catNow<castMax; castNow++) {
    57.         print(Time.time); //check time prior to yield
    58.         yield WaitForSeconds(0.1);
    59.         print(Time.time); //check time after yield, make sure we are yielding.
    60.         if (castNow == castMax){
    61.             castingSpell = false;
    62.             castNow = 0;
    63.        
    64.             if(instantiated == true)
    65.                 Destroy(instParticle.gameObject);
    66.         }
    67.     }
    68.    
    69. }
    70.  
    71. function CastingSpellMSG(){
    72.     GUI.Label(Rect (50, 10, 100, 100), "Already Casting Spell");
    73.     yield WaitForSeconds(1);
    74.     callCastingMSG = false;
    75. }
    76.  
    77. function MakeGUI(){
    78.     GUI.Box(Rect (545, 500, 210, 30), "");
    79.     GUI.Button(Rect (545, 500, castNow, 20), "");
    80. }
    should work. You want to do the destroy check in your loop so that every time you add to your castNow variable in the loop, you check the if statement to see if you reached the max, and if so, then you break out.

    Also switched you to a for loop, while it makes little difference, it's just simple.

    Hope that helps.

    Regards,
    Rob
     
  4. Spaetten1

    Spaetten1

    Joined:
    Mar 23, 2010
    Posts:
    85
    Thanks man :D Makes sense but it leaves me with one problem:
    Code (csharp):
    1. function MakeGUI(){
    2.    GUI.Box(Rect (545, 500, 210, 30), "");
    3.    GUI.Button(Rect (545, 500, castNow, 20), "");
    4. }
    Now the "castNow" variable is not available to the function MakeGUI : / And I need it to be, it is the only way it displays how far the casting process is.

    Should I just go back to the while loop or is there another solution?
     
  5. Spaetten1

    Spaetten1

    Joined:
    Mar 23, 2010
    Posts:
    85
    And it still makes a lot of instantiated objects :(
     
  6. Ezzerland

    Ezzerland

    Joined:
    Jul 1, 2010
    Posts:
    405
    didn't look at the gui bit of the script when i was editing it, to be honest :p you can declare the var for castNow at the beginning of the script as before, and just have the for loop set it to 0 when you start:
    Code (csharp):
    1. public static var castNow : int;
    2. for (castNow = 0; castNow<castMax; castNow++;) {
    3.  //etc
    4. }
    I actually thought you wanted it to do this looking at the code :p

    You need to set the instantiated bool near the top of your script because every time you call the function atm it resets the var to false.. because you're telling it to.

    Hope that helps :)
    I don't actually have unity open (at work atm) so this really is just the top of my head :p if necessary i can do some tests later.

    Regards,
    Rob
     
  7. Spaetten1

    Spaetten1

    Joined:
    Mar 23, 2010
    Posts:
    85
    Thanks again :) I have been looking at your new post and some things have already been fixed - for example the instantiate problem (stupid me) :D

    Except for one thing - it doesn't destroy "instParticle" when its done casting : / It's a NullReferanceException saying "Object reference not set to instance of an object".

    Besides that the casting bar is going all crazy, back and forth and so on, until it reaches 200 where it disappears ( as it should ^^)

    Btw It'd be really cool if you could test it out and see what kind of errors you can find - though it's an hour past midnight where I live so I may not be able to check it before tomorrow :)
     
  8. Spaetten1

    Spaetten1

    Joined:
    Mar 23, 2010
    Posts:
    85
    Ok got the casting part almost working now, it displays fine the first time you use it ^^ The only problem I have now is actually with the destroy thingy :)

    *EDIT* got the casting thing working completely now
     
  9. Ezzerland

    Ezzerland

    Joined:
    Jul 1, 2010
    Posts:
    405
    Good to hear. In the event others have this issue in the future and search/find this, would you post your *fixed* code copy?

    Good luck on your game!
    Rob
     
  10. Spaetten1

    Spaetten1

    Joined:
    Mar 23, 2010
    Posts:
    85
    Yes of course - here is the code:
    Code (csharp):
    1. // The effect that comes during casting time, not the spell fired
    2. var castParticle : Transform;
    3.  
    4. // Put the name of the specific spell to make it print in the cast bar
    5. var spellName = "";
    6.  
    7. var mySkin : GUISkin;
    8.  
    9. var boxRect = 100;
    10.  
    11. var instantiated : boolean = false;
    12.  
    13. static var castMax = 200;
    14.  
    15. var castNow : int = 0; // Dont mess with that
    16.  
    17. private var castingSpell = false;
    18. var callCastingMSG = false;
    19.  
    20. // The skin that will be used a couple of seconds after done casting
    21. var callDoneSkin = false;
    22.  
    23. function Update(){
    24.     if(castNow == castMax){
    25.         animation.Play("CastSpell");
    26.         castNow = 0;
    27.        
    28.         callDoneSkin = true;
    29.     }
    30.        
    31.        
    32.     if(Input.GetMouseButtonDown(1)  castingSpell == false){
    33.         castingSpell = true;
    34.     }
    35.    
    36.     else if(Input.GetMouseButtonDown(1)  castingSpell == true){
    37.         callCastingMSG = true;
    38.     }
    39.    
    40.     if(castingSpell == true){
    41.         CastFunction();
    42.     }
    43. }
    44.        
    45. function OnGUI () {
    46.     GUI.Box(Rect (10, 10, boxRect, 30), "Health: " + boxRect);
    47.    
    48.     if(callCastingMSG == true){
    49.         CastingSpellMSG();
    50.     }
    51.    
    52.     if(castingSpell == true){
    53.         GUI.skin = null;
    54.         MakeGUI();
    55.     }
    56.    
    57.     if(callDoneSkin == true){
    58.         GUI.skin = mySkin;
    59.         DoneSkin();
    60.     }
    61. }
    62.  
    63. function OnMouseEnter(){
    64.     boxRect -= 10;
    65. }
    66.  
    67. function CastFunction(){
    68.     if(instantiated == false){
    69.         var instParticle = Instantiate(castParticle, transform.position, transform.rotation);
    70.         instantiated = true;
    71.     }
    72.    
    73.    
    74.    
    75.     for (castNow = 0; castNow<castMax; castNow++){
    76.         yield WaitForSeconds(0.001);
    77.         animation.CrossFade("CastTime");
    78.         animation.wrapMode = WrapMode.Loop;
    79.     }
    80.  
    81.     if (castNow == castMax){
    82.         castingSpell = false;
    83.         if(instantiated == true){
    84.             Destroy(instParticle.Transform);
    85.         }
    86.     }
    87. }
    88.  
    89. function CastingSpellMSG(){
    90.     GUI.Label(Rect (50, 10, 100, 100), "Already Casting Spell");
    91.     yield WaitForSeconds(1);
    92.     callCastingMSG = false;
    93. }
    94.  
    95. function MakeGUI(){
    96.     GUI.Box(Rect (540, 495, 210, 30), spellName);
    97.     GUI.Button(Rect (545, 500, castNow, 20), "");
    98. }
    99.  
    100. function DoneSkin(){
    101.     GUI.Box(Rect (540, 495, 210, 30), spellName);
    102.     GUI.Button(Rect (545, 500, 200, 20), "");
    103.     yield WaitForSeconds(1.5);
    104.     callDoneSkin = false;
    105. }
    I threw in some animations and a littl GUI effect when the casting is finished.
    Now my only problem is that I can't destroy the particles when it's done casting- and I am trying to figure out a way to fade out the GUI. If anyone can give me a point at where I should look it'd be great :D

    Besides that, thanks for helping me out Ezzerland :)
     
  11. Spaetten1

    Spaetten1

    Joined:
    Mar 23, 2010
    Posts:
    85
    This game is getting really annoying!
    I am trying to make all my new animations work in different situations, but my machine won't agree with me :(

    My "Control" script is
    Code (csharp):
    1. static var jumpAble = false;
    2. static var doubleJump = true;
    3.  
    4. var moveSpeed : int = 5.0;
    5.  
    6. function Update () {
    7.        
    8.     if(Physics.Raycast (transform.position, -Vector3.up, 3)){
    9.         doubleJump = true;
    10.         jumpAble = true;
    11.        
    12.         if(jumpAble == false){
    13.             animation.CrossFade("Land");
    14.             animation.wrapMode = WrapMode.Once;
    15.         }
    16.     }
    17.    
    18.     else {
    19.         jumpAble = false;
    20.     }
    21.    
    22.     if(Input.GetButtonDown("Jump")  jumpAble == true){
    23.         rigidbody.velocity.y += 10;
    24.     }
    25.    
    26.     else if (Input.GetButtonDown("Jump")  doubleJump == true){
    27.         rigidbody.velocity.y += 5;
    28.         doubleJump = false;
    29.     }
    30.    
    31.     var x = Input.GetAxis("Horizontal");
    32.     var z = Input.GetAxis("Vertical");
    33.    
    34.     transform.Translate(x,0,z * Time.deltaTime * moveSpeed);
    35.        
    36. }
    and the "healthGUI" script is the one I posted before, except I removed all the animations.

    Here is my animation script:
    Code (csharp):
    1. function Start() {
    2.     animation.wrapMode = WrapMode.Loop;
    3.     animation["CastSpell"].wrapMode = WrapMode.Once;
    4.     animation["Jump2"].wrapMode = WrapMode.Once;
    5.     animation["Land"].wrapMode = WrapMode.Once;
    6.    
    7.     animation.Stop();
    8. }
    9.  
    10. function Update () {
    11.    
    12.     if(healthGUI.castingSpell == true  healthGUI.castNow < healthGUI.castMax){
    13.         animation.CrossFade("CastTime");
    14.     }
    15.  
    16.     else if(healthGUI.castNow == healthGUI.castMax){
    17.         animation.CrossFade("CastSpell");
    18.     }
    19.        
    20.     if(Input.GetButtonDown("Jump")  Controls.jumpAble == true){
    21.         animation.CrossFade("Jump2");
    22.     }
    23.    
    24.     else if(Input.GetButtonDown("Jump")  Controls.doubleJump == true){
    25.         animation.CrossFade("Jump2");
    26.     }
    27.    
    28.     else if(Input.GetButtonDown("W")){
    29.         animation.CrossFade("Run");
    30.     }
    31.    
    32.     else
    33.         animation.CrossFade("Idle");
    34. }
    As it is very simple it should work out fine, but no animations will play except for the "Idle" animation :/

    I'm not very good at scripting so I really want this to work so I can get further in my game :)
    If you could take a look at it that'd be really great!
     
  12. Spaetten1

    Spaetten1

    Joined:
    Mar 23, 2010
    Posts:
    85
    Please help me someone
     
  13. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    Are you getting any error messages?
     
  14. Spaetten1

    Spaetten1

    Joined:
    Mar 23, 2010
    Posts:
    85
    No not really, I usually check if I can see any errors before I start posting :) It just doesn't work
     
  15. Artifactx

    Artifactx

    Joined:
    Jul 20, 2012
    Posts:
    14
    This tutorial might help you or other people out: