Search Unity

Taking MachineGun.js a bit further.

Discussion in 'Scripting' started by AaronC, Nov 5, 2007.

  1. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,552
    See Below :p
     
  2. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,552
    See below.... :wink:
     
  3. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,552
    Okay sorry to be a blowass, but this might be quite useful out there now that I've got it figured out. This script can replace your fps machine gun script, and if you tag your scene objects appropriately, you can have different particle effects for different surfaces that your "bullets" hit... You set up Tags to make this work. The top three inspector slots can be filled by using a default particle systems from the gameobject menu. Enjoy!
    Code (csharp):
    1. var SparkEmitter: GameObject;
    2. var BloodEmitter: GameObject;
    3. var DirtEmitter: GameObject;
    4. var range = 100.0;
    5. var fireRate = 0.05;
    6. var force = 10.0;
    7. var damage = 5.0;
    8. var bulletsPerClip = 40;
    9. var clips = 20;
    10. var reloadTime = 0.5;
    11. private var hitParticles : ParticleEmitter;
    12. var muzzleFlash : Renderer;
    13.  
    14. private var bulletsLeft : int = 0;
    15. private var nextFireTime = 0.0;
    16. private var m_LastFrameShot = -1;
    17.  
    18.  
    19. function Start()
    20. {
    21.     var direction = transform.TransformDirection(Vector3.forward);
    22.     var hit : RaycastHit;
    23.     if (Physics.Raycast (transform.position, direction, hit, range)){
    24.         print ("I'm looking at " + hit.transform.name);
    25.     } else {
    26.         print ("I'm looking at nothing!");
    27.     }
    28.         DirtParticles = DirtEmitter.GetComponent(ParticleEmitter);
    29.        BloodParticles = BloodEmitter.GetComponent(ParticleEmitter);
    30.         SparkParticles = SparkEmitter.GetComponent(ParticleEmitter);
    31.    
    32.     // We don't want to emit particles all the time, only when we hit something.
    33.     if (DirtParticles)
    34.         DirtParticles.emit = false;
    35.        
    36.  
    37.    
    38.     // We don't want to emit particles all the time, only when we hit something.
    39.     if (BloodParticles)
    40.         BloodParticles.emit = false;
    41.        
    42.        
    43.    
    44.     // We don't want to emit particles all the time, only when we hit something.
    45.     if (SparkParticles)
    46.         SparkParticles.emit = false;
    47.     bulletsLeft = bulletsPerClip;
    48. }
    49.  
    50. function LateUpdate()
    51. {
    52.     if (muzzleFlash)
    53.     {
    54.         // We shot this frame, enable the muzzle flash
    55.         if (m_LastFrameShot == Time.frameCount)
    56.         {
    57.             muzzleFlash.transform.localRotation = Quaternion.AxisAngle(Vector3.forward, Random.value);
    58.             muzzleFlash.enabled = true;
    59.  
    60.             if (audio)
    61.             {
    62.                 if (!audio.isPlaying)
    63.                     audio.Play();
    64.                 audio.loop = true;
    65.             }
    66.         }
    67.         // We didn't, disable the muzzle flash
    68.         else
    69.         {
    70.             muzzleFlash.enabled = false;
    71.             enabled = false;
    72.            
    73.             // Play sound
    74.             if (audio)
    75.             {
    76.                 audio.loop = false;
    77.             }
    78.         }
    79.     }
    80. }
    81.  
    82. function Fire ()
    83. {
    84.     if (bulletsLeft == 0)
    85.         return;
    86.    
    87.     // If there is more than one bullet between the last and this frame
    88.     // Reset the nextFireTime
    89.     if (Time.time - fireRate > nextFireTime)
    90.         nextFireTime = Time.time - Time.deltaTime;
    91.    
    92.     // Keep firing until we used up the fire time
    93.     while( nextFireTime < Time.time  bulletsLeft != 0)
    94.     {
    95.         FireOneShot();
    96.         nextFireTime += fireRate;
    97.     }
    98. }
    99.  
    100. function FireOneShot (){
    101.  
    102.    
    103.         DirtParticles = DirtEmitter.GetComponent(ParticleEmitter);
    104.        BloodParticles = BloodEmitter.GetComponent(ParticleEmitter);
    105.         SparkParticles = SparkEmitter.GetComponent(ParticleEmitter);
    106.     var direction = transform.TransformDirection(Vector3.forward);
    107.     var hit : RaycastHit;
    108.  
    109.     // Did we hit anything?
    110.     if (Physics.Raycast (transform.position, direction, hit, range))
    111.     {
    112.         // Apply a force to the rigidbody we hit
    113.         if (hit.rigidbody)
    114.             hit.rigidbody.AddForceAtPosition(force * direction, hit.point);
    115.        
    116.         // Place the particle system for spawing out of place where we hit the surface!
    117.         // And spawn a couple of particles
    118.         if (DirtParticles&hit.collider.transform.CompareTag("Ground"))
    119.         {
    120.             DirtParticles.transform.position = hit.point;
    121.             DirtParticles.transform.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
    122.             DirtParticles.Emit();
    123.         }
    124.         if (BloodParticles&hit.collider.transform.CompareTag("Enemy"))
    125.         {
    126.             BloodParticles.transform.position = hit.point;
    127.             BloodParticles.transform.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
    128.             BloodParticles.Emit();
    129.         }
    130.        if (SparkParticles&hit.collider.transform.CompareTag("Metal"))
    131.         {
    132.             SparkParticles.transform.position = hit.point;
    133.             SparkParticles.transform.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
    134.             SparkParticles.Emit();
    135.         }
    136.         // Send a damage message to the hit object         
    137.         hit.collider.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
    138.     }
    139.    
    140.     bulletsLeft--;
    141.  
    142.     // Register that we shot this frame,
    143.     // so that the LateUpdate function enabled the muzzleflash renderer for one frame
    144.     m_LastFrameShot = Time.frameCount;
    145.     enabled = true;
    146.    
    147.     // Reload gun in reload Time       
    148.     if (bulletsLeft == 0)
    149.         Reload();          
    150. }
    151.  
    152. function Reload () {
    153.  
    154.     // Wait for reload time first - then add more bullets!
    155.     yield WaitForSeconds(reloadTime);
    156.  
    157.     // We have a clip left reload
    158.     if (clips > 0)
    159.     {
    160.         clips--;
    161.         bulletsLeft = bulletsPerClip;
    162.     }
    163. }
    164.  
    165. function GetBulletsLeft () {
    166.     return bulletsLeft;
    167. }
     
  4. rjburnett

    rjburnett

    Joined:
    Jun 27, 2007
    Posts:
    17
    targos: great script!

    can you help me with a noob question? do I attach the script to the GameObject with the Camera?
     
  5. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,552
    Hiya!

    If you open the FPS example project, you will see where the machinegun.js script is located in the FPS/PLAYER controller hieracy. you can swap that one for mine, but you will additionally have to change some scene objects:

    Define The Tags in the Machinegun1.js script.
    Tag scene objects appropriately in the inspector.

    Heres an example. Remember to have particle systems as children of the player(or somewhere in the scene. The rocketlauncher script is purposely not there, and will throw an error, but thats pretty self explanatory.

    Have fun, let me know if it doesnt work.
    AC
     

    Attached Files:

  6. rjburnett

    rjburnett

    Joined:
    Jun 27, 2007
    Posts:
    17
    thanks for the reply. was getting ahead of myself. the FPS tutorial is a good idea. same advice from our unity programmer at work.

    you are a great source of knowledge and most willing to share. it's easy to see why you are so popular in the forums.

    -fawning sycophant
     
  7. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,552
    Thanks for the compliments, though Im still not sure what fawning sycophant means. This is the first online forum I've ever been involved in, and I've learned that if you piss people off, they tend to stay pissed off for a really long time.

    Thats life huh, I shouldve hung round on the Torque forum while I learned some manners..

    Its cool that you have a Unity programmer at work. I wish big studios would adopt Unity so my skillset is more employable. The better I get with Unity, the more niche my skillset gets.

    So yeah if anyones got any paying work? :p

    Untill then I'll just keep working on my game until its playable/sellable

    It's a massive job finishing a solo project!
    Cheers
    AC
     
  8. rjburnett

    rjburnett

    Joined:
    Jun 27, 2007
    Posts:
    17
    we are working at a university making simulations. currently, we are creating a beermaking sim and have started modeling the brewery on campus.

    Right now, there is no real money, but if the prototype is good enough, it may attract backing from large beer company or grant money.

    idea is to work through steps in beer-making at a mid-scale brewery, like levels in a game. then get master brewer status and enter different challenges, like: brew a specific type of beer by choosing methods and ingredients, increase the capacity of brewery, or some kind of interactive speadsheet type sim where you have to keep a competetive brewery in business.

    it's a big idea, for now, we'd just like to make an immersive environment and prototype some exercises or challenges in the beer-making process.

    an example of an exercise might be a demonstration of a hydrometer with some quiz material attached.
     
  9. mattconley2011

    mattconley2011

    Joined:
    Feb 22, 2010
    Posts:
    105
    Particles are not showing for me
     
  10. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,552
    Hi Matt,

    Click the www button below my post and it will take you to my website. Click the stuff tab, and theres and example unityPackage.

    Have fun

    AaronC
     
  11. SomebodyZA

    SomebodyZA

    Joined:
    May 8, 2010
    Posts:
    168