Search Unity

[Solved] Prefab instantiate's rotation value wont be used by clients

Discussion in 'Multiplayer' started by Gunging, Dec 12, 2016.

  1. Gunging

    Gunging

    Joined:
    Sep 6, 2016
    Posts:
    139
    So, everything is fine, the host and clients rotate perfectly for everyone, the host fires and the proyectile takes the desired direction, which is the host's facing direction, but the clients no matter what, when fire, the proyectile always move directly up. by its code I assume it is not spawned rotated, I dont really know what might this be.

    My 2D game player has the following code to fire:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.Networking;
    4.  
    5. public class Movement : NetworkBehaviour {
    6.     private float fireRate;                          //A number
    7.     public GameObject shtPrefab;           //The proyectile prefab (abreviation from shootPrefab)
    8.     [SyncVar]private Quaternion rot;        //A quaternion
    9.  
    10.     void Update () {
    11.         if (!isLocalPlayer) { return; }           //You know what this one is for
    12.         rot = transform.rotation;                 //Sync Var rot is set to the current rotation
    13.         if (Input.GetKeyDown(KeyCode.Space)) { CmdFire(); } //If you press spacebar you fire
    14.      
    15.     }
    16.  
    17.     [Command] void CmdFire()
    18.     {
    19.         if (Time.time >= fireRate)
    20.         {
    21.             GameObject proyectile = (GameObject)Instantiate(shtPrefab, transform.position, rot); //Instantiates the proyectile shtPrefab at current position and, rotation, as rot is supposed to be the current rotation. Is here the error?
    22.             fireRate = Time.time + 0.5f; //Makes you unable to fire as fast as you can click, only every half a sec.
    23.             NetworkServer.Spawn(projectile); //Spawns the proyectile to everyone
    24.             Physics2D.IgnoreCollision(projectile.GetComponent<CircleCollider2D>(), GetComponent<BoxCollider2D>()); // Ignores the collision with the proyectile
    25.         }
    26.     }
    27. }
    and the shtPrefab contains the following script:

    Code (CSharp):
    1.     void Update()
    2.     {
    3.         GetComponent<Rigidbody2D>().velocity = transform.up * MoveSpeed;
    4.         GetComponent<Rigidbody2D>().angularVelocity = 0;
    5.     }
    Here is a picture of the player's and shoot's components:



    I am kinda new to multiplayer, so If its a really obvious mistake, sorry xd.

    Thank you for any help :3
     
    Last edited: Dec 13, 2016
  2. Jos-Yule

    Jos-Yule

    Joined:
    Sep 17, 2012
    Posts:
    292
    you have the Quat as a SyncVar, but you have to send updates to the sever via a Cmd - changing the value on the client _does not_ update it one the server. So, in your Update() method, you need to add something like this to replace line 12:
    Code (CSharp):
    1. CmdUpdateRotation(transform.rotation);
    Then add something like:
    Code (CSharp):
    1. [Command]
    2. void CmdUpdateRotation(Quat q)
    3. {
    4.     rot = q;
    5. }
     
    Gunging likes this.
  3. Jos-Yule

    Jos-Yule

    Joined:
    Sep 17, 2012
    Posts:
    292
    You could also pass the Quat in the call to CmdFire... That would only update it when you really need it, rather then every frame...
     
    Gunging likes this.
  4. Gunging

    Gunging

    Joined:
    Sep 6, 2016
    Posts:
    139
    Wow thank you, I am starting to understand this UNet thing :) It works now