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

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:

using UnityEngine;
using System.Collections;
using UnityEngine.Networking;

public class Movement : NetworkBehaviour {
    private float fireRate;                          //A number
    public GameObject shtPrefab;           //The proyectile prefab (abreviation from shootPrefab)
    [SyncVar]private Quaternion rot;        //A quaternion

    void Update () {
        if (!isLocalPlayer) { return; }           //You know what this one is for
        rot = transform.rotation;                 //Sync Var rot is set to the current rotation
        if (Input.GetKeyDown(KeyCode.Space)) { CmdFire(); } //If you press spacebar you fire
      
    }

    [Command] void CmdFire()
    {
        if (Time.time >= fireRate)
        {
            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?
            fireRate = Time.time + 0.5f; //Makes you unable to fire as fast as you can click, only every half a sec.
            NetworkServer.Spawn(projectile); //Spawns the proyectile to everyone
            Physics2D.IgnoreCollision(projectile.GetComponent<CircleCollider2D>(), GetComponent<BoxCollider2D>()); // Ignores the collision with the proyectile
        }
    }
}

and the shtPrefab contains the following script:

    void Update()
    {
        GetComponent<Rigidbody2D>().velocity = transform.up * MoveSpeed;
        GetComponent<Rigidbody2D>().angularVelocity = 0;
    }

Here is a picture of the player’s and shoot’s components:

Player

Shoot

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

Thank you for any help :3

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: CmdUpdateRotation(transform.rotation);

Then add something like:

[Command]
void CmdUpdateRotation(Quat q)
{
    rot = q;
}
1 Like

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…

1 Like

Wow thank you, I am starting to understand this UNet thing :slight_smile: It works now