Converting my script to networking

Hi guys, i wrote a script which is working on singleplayer but when i make my my game multiplayer, it doesn’t work. I can not see particle effects and other issues occures. Can you help me to make my script for multiplayer game ? I shared my script below here so you can check it and tell me what i should add.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class Particle : NetworkBehaviour
{
public Rigidbody particle;
public Transform barrel;
public GameObject impactEffect;
public float speed;
public float fireRate;
private float nextFire;

[SerializeField] private Camera cam;

void Start()
{
if(cam= null)
{
Debug.LogError(“No camera referenced!”);
this.enabled = false;
}
}

void Update()
{
if (Input.GetButton(“Fire1”) && Time.time > nextFire)
{
nextFire = Time.time + fireRate;
CmdinstanceParticles();
}
}

[Command]
void CmdinstanceParticles()
{
particle = Instantiate(particle, barrel.position, Quaternion.identity) as Rigidbody;
Vector3 shootDir = transform.forward;
particle.AddForce(shootDir * speed);
}
}

It isn’t as simple as adding a few code tags and a script becomes multiplayer. :slight_smile:

If you use the Asset Store Network Lobby, then you need to create a lobby and have users enter and join a game. You need to think about host, server and client connections. ( My personal recommendation, however, is to use Photon for networking instead. )

Have you done all that? Have you worked through some networking tutorials already to see how things hang together?

1 Like

You have you sync everything and use RPC like calls and have network views and code in if view is mine etc. Pretty much all networking kits will require something like this.

You could waste your time learning photon which in the end will cost you but is free up to 20 ccu and there is enough tutorials for you get started quickly and gain understanding on networking. But unless you fork over $$$ it’s a waste.
I would also avoid PhotonBolt, its crapware as well, its a paid trial and pay to use.

You could waste your time with UNET which is still not worth using IMO. This may change in the future but I still believe its a long ways from production ready.

I would look at DarkNetworking. It’s an up and coming networking which is unlimited CCU and has a free version and its growing in support.

It all depends on your needs.

1 Like

What about [Command] and [Client] ? what are those? They are used in tutorials but i didn’t use.

Those are UNET RPC’s.

The [Command] attribute is called by a client (!) and executed by the server on that same object. The client can only call this on objects it has authority over.

The [Client] attribute is called on the server and executed on all of the clients that are currently connected to the server for a given object.

Here is Unity’s Explantion

1 Like