How to implement a Networking Line Renderer in my Script?
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;
Answers
-
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.
0