Unity Make Laser Pointer Visible to All Clients

Updated on August 13, 2017 in [A] Multiplayer
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
10 on July 26, 2017

Does anyone know how to make a laser pointer visible to all clients in the game?. I’ve tried using a [Command] but that didn’t work. I’ve heard some solutions about using [ClientRpc] but I’m not sure how to do that. I’ve tried using this solution, but that also didn’t work (I ran OnBeamActive() in the Update() function under an if statement which changed isBeam whenever the local player pressed ‘q’. Any ideas and suggestions are appreciated. Thanks.

  • Liked by
Reply
9 on July 26, 2017

The solution you posted seems to be fine to me, especially since they say they got it working too. Maybe you could post your scripts and explain what isn’t working more?
Anyway, seeing your scripts would help us out a lot.

on July 28, 2017

So in my update function I have: 

if (Input.GetKeyDown(KeyCode.Q))
     isBeam = !isBeam;
OnBeamActive(isBeam);

and my OnBeamActive function is:

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 == false)
 { // 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);//target is a Transform referring to the end point of the beam/line. Initialize it the way you want
 }

and I have 

[SyncVar(hook = "OnBeamActive")]
 public bool isBeam = false;

The laser’s only visible on to the local player. I just start host using the NetworkManager and connect on a build, and activate the laser on the host. The laser works for the host but the build client doesn’t see anything.

Helpful
on July 28, 2017

I’m not familiar with online functions in Unity, but I think the script that will use the OnBeamActive(bool isOn) is disabled,  if you can sync the variable isBeam or the function OnBeamActive than it should work on other clients aswel.

on July 29, 2017

Well the script itself definitely isn’t completely disabled because it at least works for the local player, but not sure what could be disabling/blocking complete function.

on July 29, 2017

Maybe it’s got something to do with the object i’m attaching it to? Right now I’ve got an empty game object with local player authority and a line renderer  that’s already on the scene (it’s there before I hit Host or something) with a particles/additive shader on it. Maybe i need to spawn in multiple invisible game object?

-Edit-
Now saying that I feel like a complete idiot. I need to test this out.

on July 30, 2017

So it works on separately spawned in LPA objects, though the problem now is that the laser on every player will turn on at the same time, and I would really like them to be able to be separately turned on/off, and that the laser does not move after it is turned on.

on July 30, 2017

Alright, I half managed to fix the laser not moving part, but the problem now is that when the laser is on, the host can see the client’s laser moving, but the client can’t see the host’s laser moving. Any idea how this is happening?

Helpful
on August 2, 2017

Hard to tell without the code 😉 But do you sync the movement of the laser object?

on August 13, 2017

Well I managed to get it working, but when I switched the X axis rotation of my player prefab to be based of the camera, I had to switch the transform.forward part of my end position of the laser to be cam.transform.forward. But somehow that broke my method cause now when I turn on the lasers, they’re all outa whack, and when I reversed my code back to the old code, it was still outa whack. The laser on the local player will be fine, but the laser of other players will be pointed at the wrong angle. But what’s even more strange is that if I move the laser of the other player, on the local players screen, the laser will jump between the correct angle and the incorrect angle. The Function is still more or less the same:

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 == false)
 { // If "isOn" is false, you could disable "Beam" here using Beam.enabled = false
 return;
 }
 if (isOn && Beam.enabled == false)
 Beam.enabled = isBeam;
 isBeam = isOn;
 Beam.positionCount = 2;
 Beam.startWidth = 0.5f;
 Beam.endWidth = 0.5f;
 Beam.SetPosition(0, laserStart);
 Beam.SetPosition(1, laserStart + transform.forward * 500f);
 }

The whole thing is now called in LateUpdate() and laserStart is set as transform.position – transform.up;

on August 13, 2017

On further testing it looks like the end position of the other players’ lasers on the local player is set around the time the other player loads in and then while the other players are moving, their laser end point jumps between that set position and the updated position.

Show more replies
  • Liked by
Reply
Cancel