- Home /
Syncing/Instantiating a LineRenderer on Network
So in my project I'm am using laser pointers as a feature that I want to be able to be seen by every player on the game. However, right now, the local player can only see his own laser and no one else's. I read some ideas on using [ClientRpc] do instantiate the lasers though I do not know how to do that, so any suggestions would be appreciated. Right now I'm using [SyncVar] as such:
[SyncVar(hook = "OnBeamActive")] public bool isBeam = false;
public void OnBeamActive(bool isOn)
{
LineRenderer Beam = GetComponent<LineRenderer>(); // LineRenderer has to be attached to the object to which this script is attached to
if (!isOn)
{ // If "isOn" is false, you could disable "Beam" here using Beam.enabled = false
return;
}
isBeam = isOn;
Beam.positionCount = 2;
Beam.startWidth = 0.5f;
Beam.endWidth = 0.5f;
Beam.enabled = true;
Beam.SetPosition(0, transform.position);
Beam.SetPosition(1, transform.position + transform.forward * 500f);
}
which I've attached to my player prefab. This solution is from here but it doesn't work for me for some reason. Any ideas?
Answer by Zefalcon · Aug 24, 2017 at 01:18 AM
I just worked this out myself for a project I'm working on.
The only solution I could come up with is to have your LineRenderer be its own separate object and then Spawn it through the network. To do this, your prefab has to have a NetworkIdentity component, and be dragged into the "Spawnable Prefabs" section of your NetworkManager script.
Then, whenever you want to spawn it, you send a Command to the server to Instantiate that prefab and then spawn it on the network.
Something like this:
[Command]
void CmdSpawnBeam(){
GameObject beam = Instantiate(beamPrefab);
NetworkServer.Spawn(beam);
}
Answer by abdoawwad · Sep 25, 2021 at 02:25 PM
@Zefalcon I have the same setup as yours (as in making my laser a prefab and spawning it over the network) and it works fine .. however.. setting the position of the first and second point of my line renderer only seems to work on the server.. it doesn't get synced over to my clients.. the code to set the points positions is run on the server only and the laser prefab has a network identity attached... I thought I needed a component like Network-Transform or Network-RigidBody2D to sync the data over to the clients but there isn't such component for a line renderer