Search Unity

how do you make a fps the the gun kill other players

Discussion in 'Multiplayer' started by alzan12, Sep 10, 2010.

  1. alzan12

    alzan12

    Joined:
    Apr 6, 2010
    Posts:
    71
    how do i make my gun kill other players? right now thay can kill but on the killed players screen thay are'nt dead.


    health

    Code (csharp):
    1.  
    2. var hitPoints = 100.0;
    3. var deadReplacement : Transform;
    4. var dieSound : AudioClip;
    5.  
    6. function ApplyDamage (damage : float) {
    7.     // We already have less than 0 hitpoints, maybe we got killed already?
    8.     if (hitPoints <= 0.0)
    9.         return;
    10.  
    11.     hitPoints -= damage;
    12.     if (hitPoints <= 0.0)
    13.     {
    14.         Detonate();
    15.     }
    16. }
    17.  
    18. function Detonate () {
    19.     // Destroy ourselves
    20.     Destroy(gameObject);
    21.    
    22.     // Play a dying audio clip
    23.     if (dieSound)
    24.         AudioSource.PlayClipAtPoint(dieSound, transform.position);
    25.  
    26.     // Replace ourselves with the dead body
    27.     if (deadReplacement) {
    28.         var dead : Transform = Instantiate(deadReplacement, transform.position, transform.rotation);
    29.        
    30.         // Copy position  rotation from the old hierarchy into the dead replacement
    31.         CopyTransformsRecurse(transform, dead);
    32.     }
    33. }
    34.  
    35. static function CopyTransformsRecurse (src : Transform,  dst : Transform) {
    36.     dst.position = src.position;
    37.     dst.rotation = src.rotation;
    38.    
    39.     for (var child : Transform in dst) {
    40.         // Match the transform with the same name
    41.         var curSrc = src.Find(child.name);
    42.         if (curSrc)
    43.             CopyTransformsRecurse(curSrc, child);
    44.     }
    45. }

    gun


    Code (csharp):
    1.  
    2.  
    3. var range = 100.0;
    4. var fireRate = 0.05;
    5. var force = 10.0;
    6. var damage = 5.0;
    7. var bulletsPerClip = 40;
    8. var clips = 20;
    9. var reloadTime = 0.5;
    10. private var hitParticles : ParticleEmitter;
    11. var muzzleFlash : Renderer;
    12.  
    13. private var bulletsLeft : int = 0;
    14. private var nextFireTime = 0.0;
    15. private var m_LastFrameShot = -1;
    16.  
    17. function Start () {
    18.     hitParticles = GetComponentInChildren(ParticleEmitter);
    19.    
    20.     // We don't want to emit particles all the time, only when we hit something.
    21.     if (hitParticles)
    22.         hitParticles.emit = false;
    23.     bulletsLeft = bulletsPerClip;
    24. }
    25.  
    26. function LateUpdate() {
    27.     if (muzzleFlash) {
    28.         // We shot this frame, enable the muzzle flash
    29.         if (m_LastFrameShot == Time.frameCount) {
    30.             muzzleFlash.transform.localRotation = Quaternion.AngleAxis(Random.value * 360, Vector3.forward);
    31.             muzzleFlash.enabled = true;
    32.  
    33.             if (audio) {
    34.                 if (!audio.isPlaying)
    35.                     audio.Play();
    36.                 audio.loop = true;
    37.             }
    38.         } else {
    39.         // We didn't, disable the muzzle flash
    40.             muzzleFlash.enabled = false;
    41.             enabled = false;
    42.            
    43.             // Play sound
    44.             if (audio)
    45.             {
    46.                 audio.loop = false;
    47.             }
    48.         }
    49.     }
    50. }
    51.  
    52. function Fire () {
    53.     if (bulletsLeft == 0)
    54.         return;
    55.    
    56.     // If there is more than one bullet between the last and this frame
    57.     // Reset the nextFireTime
    58.     if (Time.time - fireRate > nextFireTime)
    59.         nextFireTime = Time.time - Time.deltaTime;
    60.    
    61.     // Keep firing until we used up the fire time
    62.     while( nextFireTime < Time.time  bulletsLeft != 0) {
    63.         FireOneShot();
    64.         nextFireTime += fireRate;
    65.     }
    66. }
    67.  
    68. function FireOneShot () {
    69.     var direction = transform.TransformDirection(Vector3.forward);
    70.     var hit : RaycastHit;
    71.    
    72.     // Did we hit anything?
    73.     if (Physics.Raycast (transform.position, direction, hit, range)) {
    74.         // Apply a force to the rigidbody we hit
    75.         if (hit.rigidbody)
    76.             hit.rigidbody.AddForceAtPosition(force * direction, hit.point);
    77.        
    78.         // Place the particle system for spawing out of place where we hit the surface!
    79.         // And spawn a couple of particles
    80.         if (hitParticles) {
    81.             hitParticles.transform.position = hit.point;
    82.             hitParticles.transform.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
    83.             hitParticles.Emit();
    84.         }
    85.  
    86.         // Send a damage message to the hit object         
    87.         hit.collider.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
    88.     }
    89.    
    90.     bulletsLeft--;
    91.  
    92.     // Register that we shot this frame,
    93.     // so that the LateUpdate function enabled the muzzleflash renderer for one frame
    94.     m_LastFrameShot = Time.frameCount;
    95.     enabled = true;
    96.    
    97.     // Reload gun in reload Time       
    98.     if (bulletsLeft == 0)
    99.         Reload();          
    100. }
    101.  
    102. function Reload () {
    103.  
    104.     // Wait for reload time first - then add more bullets!
    105.     yield WaitForSeconds(reloadTime);
    106.  
    107.     // We have a clip left reload
    108.     if (clips > 0) {
    109.         clips--;
    110.         bulletsLeft = bulletsPerClip;
    111.     }
    112. }
    113.  
    114. function GetBulletsLeft () {
    115.     return bulletsLeft;
    116. }
    117.  
    118.  
     
  2. AkilaeTribe

    AkilaeTribe

    Joined:
    Jul 4, 2010
    Posts:
    1,149
    You went to the point where players can connect, yet you can't make a player die on all users application...?

    You need to make a RPC call, telling all users to make a character die.
     
  3. alzan12

    alzan12

    Joined:
    Apr 6, 2010
    Posts:
    71
    how do i do that? :?:
     
  4. AkilaeTribe

    AkilaeTribe

    Joined:
    Jul 4, 2010
    Posts:
    1,149
  5. alzan12

    alzan12

    Joined:
    Apr 6, 2010
    Posts:
    71
    thanks. is this right?

    Code (csharp):
    1.  
    2.  
    3.  
    4.  
    5.  
    6. var maximumHitPoints = 100.0;
    7. var hitPoints = 100.0;
    8.  
    9. var ai = false;
    10. var deadReplacement : Transform;
    11.  
    12. var bulletGUI : GUIText;
    13. var rocketGUI : DrawRockets;
    14. var healthGUI : GUITexture;
    15.  
    16. var slideclip : AudioClip[];
    17. var range = 100.0;
    18. var audioslideLength = 0.3;
    19. var slideing = false;
    20. var walking = false;
    21. var hitsomthig = false;
    22.  
    23. var walkSounds : AudioClip[];
    24. var painLittle : AudioClip;
    25. var painBig : AudioClip;
    26. var die : AudioClip;
    27. var audioStepLength = 0.3;
    28.  
    29. private var machineGun : MachineGun;
    30. private var rocketLauncher : RocketLauncher;
    31. private var healthGUIWidth = 0.0;
    32. private var gotHitTimer = -1.0;
    33.  
    34. var rocketTextures : Texture[];
    35.  
    36. function soundtoplay() {
    37.         var direction = transform.TransformDirection(-Vector3.up);
    38.         var hit2 : RaycastHit;
    39.         if (Physics.Raycast (transform.position, direction, hit2, range)) {
    40.         hitsomthig = true;
    41.    
    42.              //yield WaitForSeconds(audio.clip.length);
    43.            
    44.         }
    45.         if (hit2.collider.tag == "Slide") {
    46.         walking = false;
    47.         slideing = true;
    48.         }
    49.     }
    50.  
    51.  
    52.  
    53. static function CopyTransformsRecurse (src : Transform,  dst : Transform) {
    54.     dst.position = src.position;
    55.     dst.rotation = src.rotation;
    56.    
    57.     for (var child : Transform in dst) {
    58.         // Match the transform with the same name
    59.         var curSrc = src.Find(child.name);
    60.         if (curSrc)
    61.             CopyTransformsRecurse(curSrc, child);
    62.     }
    63. }
    64.  
    65. function Awake () {
    66.     machineGun = GetComponentInChildren(MachineGun);
    67.     rocketLauncher = GetComponentInChildren(RocketLauncher);
    68.  
    69.     PlayStepSounds();
    70.  
    71.     healthGUIWidth = healthGUI.pixelInset.width;
    72.     networkView.RPC ("Die", RPCMode.All, "you died");
    73. }
    74.  
    75. function ApplyDamage (damage : float) {
    76.     if (hitPoints < 0.0)
    77.         return;
    78.  
    79.     // Apply damage
    80.     hitPoints -= damage;
    81.  
    82.     // Play pain sound when getting hit - but don't play so often
    83.     if (Time.time > gotHitTimer  painBig  painLittle) {
    84.         // Play a big pain sound
    85.         if (hitPoints < maximumHitPoints * 0.2 || damage > 20) {
    86.             audio.PlayOneShot(painBig, 1.0 / audio.volume);
    87.             gotHitTimer = Time.time + Random.Range(painBig.length * 2, painBig.length * 3);
    88.         } else {
    89.             // Play a small pain sound
    90.             audio.PlayOneShot(painLittle, 1.0 / audio.volume);
    91.             gotHitTimer = Time.time + Random.Range(painLittle.length * 2, painLittle.length * 3);
    92.         }
    93.     }
    94.  
    95.     // Are we dead?
    96.     if (hitPoints < 0.0)
    97.         Die();
    98. }
    99.  
    100.    
    101. @RPC
    102. function Die () {
    103.     if (die)
    104.         AudioSource.PlayClipAtPoint(die, transform.position);
    105.    
    106.     // Disable all script behaviours (Essentially deactivating player control)
    107.     var coms : Component[] = GetComponentsInChildren(MonoBehaviour);
    108.     for (var b in coms) {
    109.         var p : MonoBehaviour = b as MonoBehaviour;
    110.         if (p)
    111.             p.enabled = false;
    112.     }
    113. if (!ai){
    114.     LevelLoadFade.FadeAndLoadLevel(Application.loadedLevel, Color.white, 2.0);
    115.     }
    116. if (ai) {
    117.         Destroy(gameObject);
    118.         var dead : Transform = Instantiate(deadReplacement, transform.position, transform.rotation);
    119.        
    120.         // Copy position  rotation from the old hierarchy into the dead replacement
    121.         CopyTransformsRecurse(transform, dead);
    122. }
    123. }
    124.  
    125. function LateUpdate () {
    126.     // Update gui every frame
    127.     // We do this in late update to make sure machine guns etc. were already executed
    128.     UpdateGUI();
    129. }
    130.  
    131. function PlayStepSounds () {
    132. soundtoplay();
    133.     var controller : CharacterController = GetComponent(CharacterController);
    134.  
    135.     while (true) {
    136.         if (controller.isGrounded  controller.velocity.magnitude > 0.3  walking  !slideing) {
    137.             audio.clip = walkSounds[Random.Range(0, walkSounds.length)];
    138.             audio.Play();
    139.             yield WaitForSeconds(audioStepLength);
    140.             slideing = false;
    141.         } else if (slideing) {
    142.             walking = false;
    143.             audio.clip = slideclip[Random.Range(0, slideclip.length)];
    144.             audio.Play();
    145.             yield WaitForSeconds(audioslideLength);
    146.        
    147.        
    148.        
    149.        
    150.          } else {
    151.             yield;
    152.         }
    153.     }
    154. }
    155.  
    156.  
    157. function UpdateGUI () {
    158.     // Update health gui
    159.     // The health gui is rendered using a overlay texture which is scaled down based on health
    160.     // - Calculate fraction of how much health we have left (0...1)
    161.     var healthFraction = Mathf.Clamp01(hitPoints / maximumHitPoints);
    162.  
    163.     // - Adjust maximum pixel inset based on it
    164.     healthGUI.pixelInset.xMax = healthGUI.pixelInset.xMin + healthGUIWidth * healthFraction;
    165.  
    166.     // Update machine gun gui
    167.     // Machine gun gui is simply drawn with a bullet counter text
    168.     if (machineGun) {
    169.         bulletGUI.text = machineGun.GetBulletsLeft().ToString();
    170.     }
    171.    
    172.     // Update rocket gui
    173.     // This is changed from the tutorial PDF. You need to assign the 20 Rocket textures found in the GUI/Rockets folder
    174.     // to the RocketTextures property.
    175.     if (rocketLauncher) {
    176.         rocketGUI.UpdateRockets(rocketLauncher.ammoCount);
    177.         /*if (rocketTextures.Length == 0) {
    178.             Debug.LogError ("The tutorial was changed with Unity 2.0 - You need to assign the 20 Rocket textures found in the GUI/Rockets folder to the RocketTextures property.");
    179.         } else {
    180.             rocketGUI.texture = rocketTextures[rocketLauncher.ammoCount];
    181.         }*/
    182.     }
    183. }
    184.  
    185.  
     
  6. AkilaeTribe

    AkilaeTribe

    Joined:
    Jul 4, 2010
    Posts:
    1,149
    The best way to know is to build and run... :?
     
  7. alzan12

    alzan12

    Joined:
    Apr 6, 2010
    Posts:
    71
    nope it wont work. wold it help to say im useing smart fox server. and my problums are can kill other player on one screen but not the other. cant see other players wepons. cant respond in same game and can kill ai but only on one screen. if i gave you my project files can you help debug them. im not that far into makeing my game. but i need to get this done so i can get going on it.
     
  8. AkilaeTribe

    AkilaeTribe

    Joined:
    Jul 4, 2010
    Posts:
    1,149
    I don't even know what Smart Fox Server is ...! I have my own project to do, and it do not require that thing (I only learnt Unity basic network because I only needed that) !