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

Sync Rigidbody2D doesnt work

Discussion in 'Multiplayer' started by NahuGuzmy, Nov 2, 2015.

  1. NahuGuzmy

    NahuGuzmy

    Joined:
    Aug 15, 2015
    Posts:
    14
    Hi, i have a trouble with "bullets" in my game. I cant see the other player bullets in my screen and viceversa.

    Here is my bullet prefab:



    My NetworkManager with the "bullet" registred:



    The code:

    Code (CSharp):
    1. public class Shoot : NetworkBehaviour {
    2.  
    3.     public GameObject shot;
    4.     public Transform shotSpawn;
    5.     public float fireRate = 0.5f;
    6.     public float shotForce = 1500f;
    7.     float nextFire;
    8.  
    9.     // Use this for initialization
    10.     void Start () {
    11.    
    12.     }
    13.    
    14.     // Update is called once per frame
    15.     void FixedUpdate () {
    16.         if (!isLocalPlayer)
    17.             return;
    18.  
    19.         if (Input.GetMouseButtonDown(0) && Time.time > nextFire)
    20.         {
    21.  
    22.             nextFire = Time.time + fireRate;
    23.             GameObject go = Instantiate(shot, shotSpawn.position, shotSpawn.rotation) as GameObject;
    24.             NetworkServer.Spawn (go);
    25.             Physics2D.IgnoreCollision(go.GetComponent<Collider2D>(), GetComponent<Collider2D>());
    26.             Vector3 pos = (Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position).normalized;
    27.             go.GetComponent<Rigidbody2D>().AddForce(pos * shotForce);
    28.  
    29.         }
    30.     }
    31. }
    And this is what happens:


    and viceversa


    What i am doing wrong?
     
  2. Artaani

    Artaani

    Joined:
    Aug 5, 2012
    Posts:
    423
    If I understand right, clients cannot perform NetworkServer.Spawn

    You should create function:

    Code (CSharp):
    1. [Command]
    2. void CmdFire () {
    3.     //Do shot here
    4. }
    And perform it by mouse click.
     
    NahuGuzmy likes this.
  3. NahuGuzmy

    NahuGuzmy

    Joined:
    Aug 15, 2015
    Posts:
    14
    Thank you, it work!