Search Unity

Bullets

Discussion in 'Multiplayer' started by Deleted User, Jun 12, 2016.

  1. Deleted User

    Deleted User

    Guest

    Hi everyone!
    I'm new at unity and I'm developing a multiplayer game, and I have some issues.
    When my character shoots on the server(host) the bullet spawns on the correct spot, with the correct velocity.
    But when my character shoots on the client the bullet doesn't spawn on the correct spot, and it hasn't got any velocity.

    The script is:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Networking;
    3.  
    4. public class playerControl : NetworkBehaviour {
    5.  
    6.     private float x, y;
    7.     public GameObject bulletPrefab;
    8.  
    9.     void Update ()
    10.     {
    11.         //this section controls the player
    12.         if (!isLocalPlayer)
    13.             return;
    14.         GetComponent<Rigidbody2D>().angularVelocity = 0;
    15.         GetComponent<Rigidbody2D>().velocity = new Vector2 (0,0);
    16.         x = (float)Mathf.Cos((transform.eulerAngles.z + 90) / 57.29578f);
    17.         y = (float)Mathf.Sin((transform.eulerAngles.z + 90) / 57.29578f);
    18.         if (Input.GetAxisRaw("Go") > 0)
    19.         {
    20.             GetComponent<Rigidbody2D>().velocity = new Vector2 (x * 7,y * 7);
    21.         }
    22.         if (Input.GetAxisRaw("Go") < 0)
    23.         {
    24.             GetComponent<Rigidbody2D>().velocity = new Vector2 (-x * 7,-y * 7);
    25.         }
    26.         if (Input.GetAxisRaw("Turn") < 0)
    27.         {
    28.             GetComponent<Rigidbody2D>().angularVelocity = 130;
    29.             if (Input.GetAxisRaw("Go") < 0)
    30.                 GetComponent<Rigidbody2D>().angularVelocity = -130;
    31.         }
    32.         if (Input.GetAxisRaw("Turn") > 0)
    33.         {
    34.             GetComponent<Rigidbody2D>().angularVelocity = -130;
    35.             if (Input.GetAxisRaw("Go") < 0)
    36.                 GetComponent<Rigidbody2D>().angularVelocity = 130;
    37.         }
    38.         //this section controls the shooting
    39.         if (Input.GetAxisRaw ("Fire") != 0)
    40.                 {
    41.                     CmdFire ();
    42.                 }
    43.     }
    44.     [Command]
    45.     void CmdFire()
    46.     {
    47.         var bullet = (GameObject)Instantiate (bulletPrefab, transform.position + new Vector3(x,y,0) * 1.0309f, Quaternion.identity);
    48.         bullet.GetComponent<Rigidbody2D> ().velocity = new Vector2(x * 20,y * 20);
    49.         NetworkServer.Spawn (bullet);
    50.         Destroy (bullet, 10);
    51.     }
    52.     //this section controls the local player color
    53.     public override void OnStartLocalPlayer()
    54.     {
    55.         GetComponent<SpriteRenderer> ().color = Color.green;
    56.     }
    57. }
    58.  
    Don't mind the controling and the color changeing part.
    How can I fix this?
    Sorry for my bad english.