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

Script 3 :)

Discussion in 'Scripting' started by The3DKnight, Feb 10, 2011.

  1. The3DKnight

    The3DKnight

    Joined:
    Feb 8, 2011
    Posts:
    240
    Hi can some one help me with the script i used FPS Tutorial to when i shoot my GUI bullets decrease but i added some codes to the script "MachineGun" and now the bullets aren't decrease can someone help.This is the script

    Code (csharp):
    1. var EmitSparksTag: String;
    2. var EmitBloodTag: String;
    3. var EmitDirtTag: String;
    4. var EmitWaterTag: String;
    5. var SparkEmitter: GameObject;
    6. var BloodEmitter: GameObject;
    7. var DirtEmitter: GameObject;
    8. var WaterEmitter: GameObject;
    9. var range = 100.0;
    10. var fireRate = 0.05;
    11. var force = 10.0;
    12. var damage = 5.0;
    13. var bulletsPerClip = 40;
    14. var clips = 20;
    15. var reloadTime = 0.5;
    16. private var hitParticles : ParticleEmitter;
    17. var muzzleFlash : Renderer;
    18.  
    19. private var bulletsLeft : int = 0;
    20. private var nextFireTime = 0.0;
    21. private var m_LastFrameShot = -1;
    22.  
    23.  
    24. function Start()
    25. {
    26.  
    27.     var direction = transform.TransformDirection(Vector3.forward);
    28.     var hit : RaycastHit;
    29.     if (Physics.Raycast (transform.position, direction, hit, range)){
    30.         print ("I'm looking at " + hit.transform.name);
    31.     } else {
    32.         print ("I'm looking at nothing!");
    33.     }
    34.         DirtParticles = DirtEmitter.GetComponent(ParticleEmitter);
    35.        BloodParticles = BloodEmitter.GetComponent(ParticleEmitter);
    36.         SparkParticles = SparkEmitter.GetComponent(ParticleEmitter);
    37.     WaterParticles = WaterEmitter.GetComponent(ParticleEmitter);
    38.    
    39.     // We don't want to emit particles all the time, only when we hit something.
    40.     if (DirtParticles)
    41.         DirtParticles.emit = false;
    42.        
    43.  
    44.    
    45.     // We don't want to emit particles all the time, only when we hit something.
    46.     if (BloodParticles)
    47.         BloodParticles.emit = false;
    48.        
    49.    
    50.     // We don't want to emit particles all the time, only when we hit something.
    51.     if (WaterParticles)
    52.     WaterParticles.emit = false;   
    53.        
    54.    
    55.     // We don't want to emit particles all the time, only when we hit something.
    56.     if (hitParticles)
    57.         hitParticles.emit = false;
    58.     bulletsLeft = bulletsPerClip;
    59. }
    60.  
    61. function LateUpdate()
    62. {
    63.     if (muzzleFlash)
    64.     {
    65.         // We shot this frame, enable the muzzle flash
    66.         if (m_LastFrameShot == Time.frameCount)
    67.         {
    68.             muzzleFlash.transform.localRotation = Quaternion.AxisAngle(Vector3.forward, Random.value);
    69.             muzzleFlash.enabled = true;
    70.  
    71.             if (audio)
    72.             {
    73.                 if (!audio.isPlaying)
    74.                     audio.Play();
    75.                 audio.loop = true;
    76.             }
    77.         }
    78.         // We didn't, disable the muzzle flash
    79.         else
    80.         {
    81.             muzzleFlash.enabled = false;
    82.             enabled = false;
    83.            
    84.             // Play sound
    85.             if (audio)
    86.             {
    87.                 audio.loop = false;
    88.             }
    89.         }
    90.     }
    91. }
    92.  
    93. function Fire()
    94. {
    95.     if (bulletsLeft == 0)
    96.         return;
    97.    
    98.     // If there is more than one bullet between the last and this frame
    99.     // Reset the nextFireTime
    100.     if (Time.time - fireRate > nextFireTime)
    101.         nextFireTime = Time.time - Time.deltaTime;
    102.    
    103.     // Keep firing until we used up the fire time
    104.     while( nextFireTime < Time.time  bulletsLeft != 0)
    105.     {
    106.         FireOneShot();
    107.         nextFireTime += fireRate;
    108.     }
    109. }
    110.  
    111. function FireOneShot () {
    112.  
    113.     var direction = transform.TransformDirection(Vector3.forward);
    114.     var hit : RaycastHit;
    115.    
    116.         // Did we hit anything?
    117.     if (Physics.Raycast (transform.position, direction, hit, range))
    118.     {
    119.         // Apply a force to the rigidbody we hit
    120.         if (hit.rigidbody)
    121.             hit.rigidbody.AddForceAtPosition(force * direction, hit.point);
    122.    
    123.         DirtParticles = DirtEmitter.GetComponent(ParticleEmitter);
    124.        BloodParticles = BloodEmitter.GetComponent(ParticleEmitter);
    125.         SparkParticles = SparkEmitter.GetComponent(ParticleEmitter);
    126.         WaterParticles = WaterEmitter.GetComponent(ParticleEmitter);
    127.        
    128.        
    129.         // Place the particle system for spawing out of place where we hit the surface!
    130.         // And spawn a couple of particles
    131.         if (DirtParticles&hit.collider.transform.CompareTag(EmitDirtTag))
    132.         {
    133.             DirtParticles.transform.position = hit.point;
    134.             DirtParticles.transform.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
    135.             DirtParticles.Emit();
    136.         }
    137.         if (BloodParticles&hit.collider.transform.CompareTag(EmitBloodTag))
    138.         {
    139.             BloodParticles.transform.position = hit.point;
    140.             BloodParticles.transform.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
    141.             BloodParticles.Emit();
    142.         }
    143.        
    144.        if (SparkParticles&hit.collider.transform.CompareTag(EmitSparksTag))
    145.         {
    146.             SparkParticles.transform.position = hit.point;
    147.             SparkParticles.transform.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
    148.             SparkParticles.Emit();
    149.         }
    150.         if (WaterParticles&hit.collider.transform.CompareTag(EmitWaterTag))
    151.         {
    152.             WaterParticles.transform.position = hit.point;
    153.             WaterParticles.transform.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
    154.             WaterParticles.Emit();
    155.         }
    156.         // Send a damage message to the hit object         
    157.         hit.collider.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
    158.     }
    159.    
    160.     bulletsLeft--;
    161.  
    162.     // Register that we shot this frame,
    163.     // so that the LateUpdate function enabled the muzzleflash renderer for one frame
    164.     m_LastFrameShot = Time.frameCount;
    165.     enabled = true;
    166.    
    167.     // Reload gun in reload Time       
    168.     if (bulletsLeft == 0){
    169.     animation.Play("Reload");
    170.         Reload();
    171.    }
    172.    
    173. }
    174. function Reload () {
    175.  
    176.     // Wait for reload time first - then add more bullets!
    177.     yield WaitForSeconds(2);
    178.  
    179.     // We have a clip left reload
    180.     if (clips > 0)
    181.     {
    182.         clips--;
    183.         bulletsLeft = bulletsPerClip;
    184.     }
    185. }
    186.  
    187. function GetBulletsLeft () {
    188.     return bulletsLeft;
    189.    
    190.    
    191.    
    192.    
    193. }
    194.  
    :)
     
  2. The3DKnight

    The3DKnight

    Joined:
    Feb 8, 2011
    Posts:
    240
    Nobody come one.....
     
  3. geetoo

    geetoo

    Joined:
    Apr 21, 2009
    Posts:
    42
    Are you sure your "Fire" function is being called ? I don't see any reference to it in your code.
     
  4. The3DKnight

    The3DKnight

    Joined:
    Feb 8, 2011
    Posts:
    240
    I don't know can you fix it for me please...................? :)
     
  5. The3DKnight

    The3DKnight

    Joined:
    Feb 8, 2011
    Posts:
    240
  6. Chris-Sinclair

    Chris-Sinclair

    Joined:
    Jun 14, 2010
    Posts:
    1,326
    3DKnight, you need to make a call to your Fire() method somewhere in your code. For example, you could put it in your Update() method and check for a mouse click like:

    Code (csharp):
    1.  
    2. if(Input.GetMouseButtonDown(0))
    3. {
    4.      Fire();
    5. }
    6.  
     
  7. The3DKnight

    The3DKnight

    Joined:
    Feb 8, 2011
    Posts:
    240
    I putted this in function Fire()

    And when i start presing mousebutton unity just crashes and it doesn't decrease my gui...
     
  8. Chris-Sinclair

    Chris-Sinclair

    Joined:
    Jun 14, 2010
    Posts:
    1,326
    I said to put it in your Update method, not Fire. You likely introduced an infinite loop by putting it in your Fire method.
     
  9. The3DKnight

    The3DKnight

    Joined:
    Feb 8, 2011
    Posts:
    240
    I made

    Code (csharp):
    1.  
    2. function Update()
    3. {
    4. if(Input.GetMouseButtonDown(0))
    5. {
    6.      Fire();
    7. }
    8. }
    9.  
    And still nothing happeneds..........
     
  10. Mike L

    Mike L

    Joined:
    Sep 14, 2010
    Posts:
    1,035
    try Input.GetButtonDown("Fire1")
     
  11. The3DKnight

    The3DKnight

    Joined:
    Feb 8, 2011
    Posts:
    240
    I've done that and it doesn't work mabe it's not to this script....I must check other scripts
     
  12. Mike L

    Mike L

    Joined:
    Sep 14, 2010
    Posts:
    1,035
    well, i do know from the fps tutorial you are using, the player weapons script sends the fire message (that starts the fire courotine) and then makes it shoot