How to implement a Networking Line Renderer in my Script?

The whole answer can be found below.

Please note: The Photon forum is closed permanently. After many dedicated years of service we have made the decision to retire our forum and switch to read-only: we've saved the best to last! And we offer you support through these channels:

Try Our
Documentation

Please check if you can find an answer in our extensive documentation on PUN.

Join Us
on Discord

Meet and talk to our staff and the entire Photon-Community via Discord.

Read More on
Stack Overflow

Find more information on Stack Overflow (for Circle members only).

Write Us
an E-Mail

Feel free to send your question directly to our developers.

How to implement a Networking Line Renderer in my Script?

EinfachFinn
2022-05-14 04:19:47

Hey Im having some trouble with my script. I want the Line Renderer to be synced but I dont know how. Even after some research I dont know how to do it. May someone please write these RPC function into my script?

using UnityEngine;

using Photon.Pun;

public class GrapplingGun : MonoBehaviour {

PhotonView PV;

[Header("AimAssist")]

[SerializeField] float aimAssistSize = 1;

[SerializeField] GameObject AssistVisualizer;



RaycastHit hit;



[Header("GrapplingHook")]

private LineRenderer lr;

public Vector3 grapplePoint;

public LayerMask whatIsGrappleable;

public Transform gunTip, camera, player;

private float maxDistance = 100f;

private SpringJoint joint;





void Awake() {

	lr = GetComponent<LineRenderer>();

	AssistVisualizer.SetActive(false);

	PV = GetComponent<PhotonView>();

}



void Update() 

{

	if(PV.IsMine)

	{

		if (Physics.SphereCast(camera.position, aimAssistSize, camera.forward, out hit, maxDistance, whatIsGrappleable)) {

		AssistVisualizer.transform.transform.position = hit.point;

		}

	

		if (Input.GetMouseButtonDown(0)) {

		StartGrapple();

		}

	

		else if (Input.GetMouseButtonUp(0)) {

		StopGrapple();

		}

	

	}

}





//Called after Update

void LateUpdate() {

	DrawRope();

}



/// <summary>

/// Call whenever we want to start a grapple

/// </summary>

void StartGrapple() {





	{

		if (Physics.SphereCast(camera.position, aimAssistSize, camera.forward, out hit, maxDistance, whatIsGrappleable)) {

			grapplePoint = hit.point;

			joint = player.gameObject.AddComponent<SpringJoint>();

			joint.autoConfigureConnectedAnchor = false;

			joint.connectedAnchor = grapplePoint;

		



			float distanceFromPoint = Vector3.Distance(player.position, grapplePoint);



			//The distance grapple will try to keep from grapple point. 

			joint.maxDistance = distanceFromPoint * 0.8f;

			joint.minDistance = distanceFromPoint * 0.25f;



			//Adjust these values to fit your game.

			joint.spring = 4.5f;

			joint.damper = 7f;

			joint.massScale = 4.5f;



			lr.positionCount = 2;

			currentGrapplePosition = gunTip.position;

			AssistVisualizer.SetActive(false);

		}

	}

}

	





/// <summary>

/// Call whenever we want to stop a grapple

/// </summary>

void StopGrapple() {

	lr.positionCount = 0;

	Destroy(joint);

	AssistVisualizer.SetActive(true);

}



private Vector3 currentGrapplePosition;

Comments

Tobias
2022-05-18 12:04:51

To render a line, you have to provide an array of points. So you could turn your drawing movement into a series of vectors and send that. Avoid sending every point on it's own and instead send a few of them every 0.x seconds instead.

Aside from using RPCs you could also setup a networked object with a PhotonView and have it call OnPhotonSerializeView() on a script of your own. I there, write / read the array and apply it to a line renderer.

Back to top