I’ve changed my shooting into a Command and it’s fine when I run it from the client that is the host but when I shoot from other clients, no bullet gets created.
I’m not sure it’s a right way to do it.
I’d go for a prefab instantiated and Spawn()'ed on server to make it appear on clients.
Just make sure you have assigned your prefab to the Spawnable list of the NetworkManager and don’t pass your prefab reference to the server - just instantiate it there and call Spawn after that.
[Command]
public void CmdFireProjectile(float Speed, int Damage, int RicochetTimes, float aimInDegrees, Vector2 velocity, float timeToLiveModifier)
{
// we instantiate the projectile at the projectileFireLocation's position.
GameObject projectile = (GameObject)Instantiate(
ProjectilePrefab.gameObject,
transform.position, /*ProjectileFireLocation.position*/
transform.rotation
);
projectile.GetComponent<BaseProjectile>().Initialize(
gameObject,
Speed,
Damage,
RicochetTimes,
aimInDegrees,
velocity,
timeToLiveModifier,
ColorTint);
projectile.GetComponent<BaseProjectile>()._speed = Speed;
projectile.gameObject.name += "_" + "Player"; // + gameObject.GetComponentInParent<PlayerController>().PlayerId;
NetworkServer.Spawn(projectile.gameObject);
But as I said, code works on the host player but does not move on the client machines. Maybe it’s the Initialize() call. Does it have to be somewhat authoritive or something? It’s just a normal method call on the projectile to initialize it. That’s my suspicion.
Just ran into a similar issue and think I have the solution. Because you’re using a custom component to control your projectile, you need to make sure that variables in that custom component that you want to be synced across the network are tagged as "SyncVar"s.
Unless you put [SyncVar] above a variable when you declare it, I don’t think anything in the Initialize function – or the line where you set the projectile speed – will actually change values anywhere except on your localHost instance.
Note that it’s fine to set your Network Send rate to zero if you want to just initialize a spawned network object with a bunch of values and then have its custom components do stuff with those values on all clients (without bothering the server again). But unless those initialization variables have a [SyncVar] tag above them in your component script, the values you’re trying to initialize them with won’t be transferred to the spawned / networked clones that the server creates on all the clients.