Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Recharging weapon only discharging.

Discussion in 'Scripting' started by everett, May 5, 2007.

  1. everett

    everett

    Joined:
    May 5, 2007
    Posts:
    1
    Here is the code first of all.
    Code (csharp):
    1. // var block
    2. var ammo_charge : int;
    3. var ammo_shot : ParticleEmitter;
    4. var ammo_take : int;
    5. var ammo_amount = 100;
    6. var ammo_text : GUIText;
    7. var mouse_button;
    8. var ammo_gain : int;
    9. // var setting block
    10. ammo_shot = GetComponentInChildren(ParticleEmitter);
    11. Screen.lockCursor = true;
    12. // function block
    13. function Update () {
    14.    
    15.     ammo_amount += ((ammo_gain * Time.deltaTime) * ammo_charge); // charge the particle beam up
    16.        
    17.     mouse_button = Input.GetMouseButton(0); // get mouse input
    18.    
    19.     switch(mouse_button) {
    20.         case true:
    21.             ammo_charge = 0; // stop charging
    22.             ammo_shot.emit = true; // turn the emitter on
    23.             ammo_amount = ammo_amount - (ammo_take * Time.deltaTime); // remove ammo per second
    24.             break;
    25.         case false:
    26.             ammo_charge = 1; // start charging
    27.             ammo_shot.emit = false; // turn the emitter off
    28.             break;
    29.     }
    30.    
    31.     if (ammo_amount <= 0) { // if the ammo is less than or equal to 0 stop fireing
    32.         ammo_shot.emit = false; // turn emitter off
    33.         ammo_amount = 0; // just incase it is less than 0
    34.     }
    35.        
    36.     if (ammo_amount > 100) { // if ammo is greater than 100 reset to 100
    37.         ammo_amount = 100;
    38.     }
    39.    
    40.     ammo_text.text = Int_to_String(Mathf.Round(ammo_amount));; // convert int to string
    41.    
    42. }
    43.  
    Now it should recharge the weapon but for some reason it does not and it always removes ammo at the same rate even when I change that rate in the script.
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    I see that ammo_amount is an integer. You're trying to add a float to it (something multiplied by Time.deltaTime). That's not going to work unless ammo_amount is a float. So either define ammo_amount as a float or use 100.0, which does the same thing.

    --Eric