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

Shooting over the network

Discussion in 'Multiplayer' started by neatgadgets, May 26, 2015.

  1. neatgadgets

    neatgadgets

    Joined:
    May 13, 2014
    Posts:
    73
    Hi,

    I am trying to get shooting working via the network. The following code works, I can see myself shoot and it hits the opponent and scores. But the opponent can't see the bullets.

    Code (CSharp):
    1.            
    2.  
    3. //the following is within an update void
    4.  
    5. if (Time.time >= timestamp && (Input.GetButtonDown("Fire1")))
    6.             {
    7.                 Rigidbody instantiatedProjectile = Instantiate(projectile, transform.position + transform.forward * 1.0f, transform.rotation) as Rigidbody;              
    8.                 instantiatedProjectile.velocity = transform.TransformDirection(new Vector3(0, 0,shootspeed));
    9.                 Destroy (instantiatedProjectile.gameObject, 0.5f);
    10.                 timestamp = Time.time + timeBetweenShots;
    11.             }
    12.  
    13.  
    What I have tried is using the following, but this seems to stop me shooting full stop. I have tried without the networkView.isMine but that doesn't help.

    Code (CSharp):
    1.      
    2.  
    3. //the following is within an update void
    4.  
    5. [URL='http://docs.unity3d.com/ScriptReference/NetworkView.html']NetworkView[/URL] networkView = GetComponent<NetworkView>();
    6. if (networkView.[URL='http://unity3d.com/support/documentation/ScriptReference/30_search.html?q=isMine']isMine [/URL]&& Time.time >= timestamp && (Input.GetButtonDown("Fire1")))
    7.             {
    8. networkView.RPC ("Shoot", RPCMode.AllBuffered);
    9.                 timestamp = Time.time + timeBetweenShots;
    10.             }
    11. ...
    12.  
    13. [RPC]
    14.      void Shoot ()
    15.      {
    16.                 Rigidbody instantiatedProjectile = Instantiate(projectile, transform.position + transform.forward * 1.0f, transform.rotation) as Rigidbody;              
    17.                 instantiatedProjectile.velocity = transform.TransformDirection(new Vector3(0, 0,shootspeed));
    18.                 Destroy (instantiatedProjectile.gameObject, 0.5f);
    19.      }
    20.  
    21.  
    22.  
     
  2. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    What you want is this:
    Code (csharp):
    1. Network.Instantiate(<object>,<Vector3 position>, <Quaternion rotation>, <group index, usually 1>);
    This does all the hard RPC work for you. :)
     
  3. neatgadgets

    neatgadgets

    Joined:
    May 13, 2014
    Posts:
    73
    Does that go into shoot function or forget about the function?

     
  4. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    Yes, wherever you're instantiating the bullet. ;)
     
  5. neatgadgets

    neatgadgets

    Joined:
    May 13, 2014
    Posts:
    73
    Hi, thanks for your reply. I gave that a go, although I do remember trying it before, and what happens is the bullets can be seen by the other player, but they just instantiate and stack up at the point of instantiation, they don't move through the air as seen by the shooter. And when I say stack up, they actually stack up on each other, very strange looking.
     
  6. dmi90

    dmi90

    Joined:
    May 20, 2015
    Posts:
    7
    I had the same, when I accidently forgot to put a Network View component on the ammo prefab I was trying to instantiate. Instantiating itself worked, but since adding the force was only happening on my side, the other client couldn't see the bullet going forward.
     
  7. luquitadenuevo

    luquitadenuevo

    Joined:
    Apr 1, 2015
    Posts:
    8
    Hi Neat,

    You could RPC the instantiation message an spawn the projectile locally (defining your physics rules locally on every computer).
    Instead of using Network.Instantiate (blah); do something like networkView.RPC ("
    InstantiateProyectile", RPCMode.All);

    And implement the full projectile instantiation (with Instantiate, NOT Network.Instantiate) and initialization (velocity set, destruction time, etc) on the RPC function.

    In this implementation you wouldn't need a NetworkView on the projectile, but some effort to make sure it's spawned consistently across all machines. For example, if you are spawning on the authoritative client (if we are not talking about IA) when the RPC reaches the server simulate the movement the projectile already made (theoretically) on the client.

    Other option is to have a script on the projectile itself that applies the physics rules on Awake / Start time, although if you are going to synch projectile states it might get bandwidth heavy if there's a lot of them.

    Hope it helps.
     
  8. Slaghton

    Slaghton

    Joined:
    Sep 9, 2013
    Posts:
    139
    In a script on the player I spawn the projectile in the correct rotation and position and then I have a script on the projectile that handles the movement.
    You can check out this guys tutorial for more help.