0
\$\begingroup\$

After a lot of searching online, I have come here as a last resort for my problem.

The question says it all. How to sync a LineRenderer?

One answer I found was to use a ClientRpc. I tried it with no effect.

Below is the snippet of my code,

    IEnumerator laserBeam()// This is called through another method on the click of a button
        {
            Beam = GetComponent<LineRenderer> ();
            Beam.SetVertexCount (2);
            Beam.material = BeamMaterial;
            Beam.SetWidth (0.07f, 0.07f);
            Beam.enabled = true;
            Beam.SetPosition (0, gameObject.transform.position);
            Beam.SetPosition (1, target.position);
            RpcDrawLines (gameObject, target.position, true);
            yield return new WaitForSeconds (1.0f);
            Beam.enabled = false;
            RpcDrawLines (gameObject, target.position, false);
        }

    [ClientRpc]
    public void RpcDrawLines(GameObject start, Vector3 end, bool isOn)
    {
        LineRenderer beam = start.GetComponent<LineRenderer> ();
        if (isOn) {
            beam.SetVertexCount (2);
            beam.material = BeamMaterial;
            beam.SetWidth (0.07f, 0.07f);
            beam.enabled = true;
            beam.SetPosition (0, start.transform.position);
            beam.SetPosition (1, end);
        } 
        else 
            {
                beam.enabled = false;
            }
        }

So, this is the code. Works fine on the Server but no effect in the client. I know I am doing something wrong in the client rpc. But I have no idea what it is. Any way of help would be appreciated. Thanks

Note: I am using UNET

\$\endgroup\$
0
\$\begingroup\$

I did it. I solved it. I was an utter idiot to not think about using [SyncVar].

Anyways, here's the solution,

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

Add this variable and set it to true on button press,

 isBeam = true; // set this to false when you want to disable the beam

Then define OnBeamActive as below,

 public void OnBeamActive(bool isOn)
    {
        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.SetVertexCount (2);
        Beam.material = BeamMaterial;
        Beam.SetWidth (0.07f, 0.07f);
        Beam.enabled = true;
        Beam.SetPosition (0, transform.position);
        Beam.SetPosition (1, target.position);//target is a Transform referring to the end point of the beam/line. Initialize it the way you want
        }

So, that's it. Make this a part of your script from where you want the line/laser beam to start from. Also, make sure you attach a LineRenderer component to this object.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.