How to fire a weapon?

Hi,

I have a prefab of my player with PhotonView component and in his bone hierarchy I have a gun attached to the spine_02. The gun has a RifleManager.cs script added to control all the gun functions and properties. The player shoots the gun with a script attached (PlayerShoot.cs), but the shooting function is in the RifleManager.cs (called from the PlayerShoot.cs script). A Projectile is spawned. I want to make it spawned in network. Do I need to add another PhotonView component to the gun attached to the player? I think, I don't. How to do that in Photon?

Best Answer

Answers

  • Hi @Tomza,

    you have at least three options for spawning projectiles. The first is to use the PhotonNetwork.Instantiate(...) function. This requires, that there is a certain prefab with an attached PhotonView component. Since firing a lot of projectiles results in a lot of objects with attached PhotonView components, this is definitely not the best approach. The second option is to use Manual Instantiation (described at the bottom of this page). This is basically the same approach, so it has the same disadvantages and you would have more work to do. You can nevertheless use this one to create a third approach: with this approach you just send a message (RPC or RaiseEvent) to each client with the position and the direction of the projectile. When receiving this information, each client can instantiate a certain projectile on his own and destroy it when it is not required any longer. There is non-essential use of networked projectiles required.
  • Hi Christian_Simone,

    Thank you for your answer. But how to do that when I need to use a communication between two scripts: one is on the player object and the second is on the gun object. The gun object is attached to the player object (to the bone). The gun script has a Fire() function with spawning an object. The function is triggered from the player script.

    Player script (PlayerShoot.cs):

    if(equipGun.weaponType == EquipGun.Weapon.rifle){
    if(GameManagerTPS.Instance.InputController.Fire1){
    rifleManager.Fire(); //CALLING THE FIRE() FUNCTION!!!!!!!!!!!!!!
    }
    }

    Rifle script (RifleManager.cs):

    void Fire(){
    GameObject bullet = Instantiate(pojectile,bulletSpawner.transform.position,bulletSpawner.transform.rotation) as GameObject;

    bullet.GetComponent().velocity = bullet.transform.forward * 1000;

    GameObject shell = Instantiate(shellBody,shellSpawner.transform.position,shellSpawner.transform.rotation) as GameObject;
    Physics.IgnoreCollision(shell.GetComponent(), playerAnimation.gameObject.GetComponent());
    shell.GetComponent().AddForce(bullet.transform.forward * forceShell);
    }
  • You can do this basically the same way you are already doing it, you just need to add another function which can be called as RPC. Whenever the client presses a certain button, you can call the RPC function on the attached PhotonView component, for example photonView.RPC("FunctionName", PhotonTargets.All);. When you have this, you need a function that can be called. To do so implement the function FunctionName and mark it with the [PunRPC] attribute. Example:
    [PunRPC]
    public void FunctionName()
    {
        // Do something
    }
    Instead of // Do something you can call the function to instantiate a projectile. This way each client in the room will instantiate a local object on his machine. If you are not familiar with RPCs I would recommend you taking a look at the RPCs and RaiseEvent documentation page.
  • But in this case, I need to add a PhotoView component to the gun. Is that OK? The gun is a child of the player that has his own PhotoView component.
  • Great, how to do that?