Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Third Party PhotonNetworking: Shooting Bullets

Discussion in 'Multiplayer' started by K0enich, Apr 14, 2014.

  1. K0enich

    K0enich

    Joined:
    Apr 14, 2014
    Posts:
    4
    Hello everybody,

    im a young student with the dream to create a little Multiplayer-Game. :)

    At the moment there are Player that can connect to the Server and flight around etc. and thats working perfectely fine.
    Now i want that the Player are able to Shoot Bullets to destroy Objects in my Scene. For testing stuff i just took a cube.



    For Shooting i just wrote a little Javascript so if i press the Button "1" it creates an Prefab which i called "theBullet" (I´m very inventive).
    Should be easy to understand i added that it plays an ShootingSound when i fire and an Ammo number that i reload when my magazine is empty which needs an certain time and an GUI which shows me the current Ammo.
    Code (csharp):
    1.  
    2. #pragma strict
    3. var theBullet : Rigidbody;
    4. var Speed = 20;
    5. var Ammo = 3;
    6. var MaxAmmo = 3;
    7. var Shoot = true;
    8. var ReloadTime = 1;
    9. var Reloading = false;
    10.  
    11. @script RequireComponent(AudioSource)
    12. var front : AudioClip;
    13. var back: AudioClip;
    14.  
    15. function Update () {
    16.    
    17.     if(Ammo == 0) {
    18.     Shoot = false;
    19.     Reloading = true;
    20.     Reload();
    21.     //animation.Play("Reload");
    22.     Ammo = MaxAmmo;
    23.     }
    24.    
    25.     if(Shoot == true){
    26.     if (Input.GetMouseButtonDown(1))
    27.     {
    28.         var clone = Instantiate(theBullet, transform.position, transform.rotation);
    29.         clone.velocity = transform.TransformDirection(Vector3(0, 0, Speed));
    30.         audio.PlayOneShot(front, 1.0);
    31.         Ammo --;
    32.         Destroy (clone.gameObject, 1);
    33.     }}
    34. }
    35.  
    36. function OnGUI() {
    37.     if(Reloading == false)
    38.         { GUI.Label (Rect (10, 10, 100, 20), "Ammo: " + Ammo); }
    39.     if(Reloading == true)
    40.         { GUI.Label (Rect (10, 10, 100, 20), "Reloading"); }
    41.  
    42.    
    43. }
    44.  
    45. function Reload() {
    46.     yield WaitForSeconds(ReloadTime);
    47.     Shoot = true;
    48.     Reloading = false;
    49. }
    50.  


    My Prefab, the Bullet, has another script where i can change the Damage and if it collides to my Object with an Health Script it takes Damage.
    Code (csharp):
    1.  
    2. #pragma strict
    3.  
    4. var Dammage = 100;
    5.  
    6. function OnCollisionEnter (info : Collision)
    7. {
    8.     info.transform.SendMessage("ApplyDammage", Dammage, SendMessageOptions.DontRequireReceiver);
    9. }
    10.  


    So if my Bullet hits an Target with an Health Script where it checks the Damage dealt by the Bullet and when the Health is 0 it gets Destroyed.
    Code (csharp):
    1.  
    2. #pragma strict
    3.  
    4. var Health = 200;
    5.  
    6. function ApplyDammage (TheDammage : int)
    7. {
    8.     Health -= TheDammage;
    9.    
    10.     if(Health <= 0)
    11.     {
    12.         Dead();
    13.     }
    14. }
    15.  
    16. function Dead()
    17. {
    18.     Destroy (gameObject);
    19. }
    20.  


    And now i stuck in errors...
    First is that if i shoot bullets on Player1, Player 2 on another PC cant see the bullets which i shoot. I added the PhotonView Script but i think its not that easy to do and yep, its not working.

    Second is that not every Player gets the current Health of the Enemy with an the HealthScript. If i shoot on the Enemy with Player 1, it should reduce its maximum health which is 200 by the damage of the bullet which is 100. We have 100 left. Now i want that the Player 2 can "see" the current health of the Enemy and if he shoots another Bullet into the Enemy, which deals another 100 Damage, it gets -100Health from the current Health and not from the maximum Health.


    Is there any easy way to fix these Problems? Or is it very complexity and takes many lines of codes and knowledge about Networking? Im just a young Boy and dont have the knowledge about that but i would love to learn some stuff about it so maybe you can recommend me any Page where i can find the Answers to my Questions? :)


    Thanks for reading my novel and forgive me if i made some Mistakes with Grammar,

    Daniel
     
  2. chrisall76

    chrisall76

    Joined:
    May 19, 2012
    Posts:
    667
    For bullet shooting, and any type of creating over network:
    1.Create a Resources folder, keep everything that will be instantiated in there.
    Now you need to use Photon's version of instantiate, also inheriting from Photon's Monobehavior.
    Sadly I don't know how this works in Javascript, but it should be similar.

    Code (csharp):
    1.  
    2. public class Shoot : Photon.MonoBehaviour {
    3.  
    4.     public GameObject arrow;
    5.  
    6.     void Update(){
    7.         if(Input.GetMouseButtonDown(1)){
    8.             PhotonNetwork.Instantiate (arrow.name, transform.position, transform.rotation, 0) as GameObject;
    9.         }
    10.     }
    11. }
    12.  
    The instaniated object needs a photonview btw.

    2.To sync enemy damage, you have to send a RPC over the network.
    Locally they would call the Damage function, which would then send a RPC to everyone else to update the damage, since
    we already damaged locally.

    Code (csharp):
    1.  
    2. public class HealthHandler : Photon.MonoBehaviour {
    3.     public float Health = 100;
    4.  
    5.     public void Damage(float Damage){
    6.          Health -= Damage/Armor;
    7.      photonView.RPC ("RPCDamage", PhotonTargets.OthersBuffered, Damage);
    8.     }
    9.     [RPC]
    10.     public void RPCDamage(float Damage){
    11.         Health -= Damage / Armor;
    12.     }
    13. }
    14.  
     
    HatrickOreally likes this.
  3. K0enich

    K0enich

    Joined:
    Apr 14, 2014
    Posts:
    4
    Big thaks to you! I didnt checked the RPC stuff out yet, ( will do it tomorrow the time is running over so fast if you have fun with programming :) ), but the Bullet stuff is working now! I got a little Error ("Only assignment, call, increment, decrement, and new object expressions can be used as a statement"), so i just deleated the "as GameObject" and Error is gone and everything working fine!
    I just write this if there will be anyone which got the same Problem then me so he will find it and the solution for it :)

    Thanks for helping me out, you dont know how i appreciate this help :)