Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Third Party Line Renderer not visible to other Players | Photon PUN 2

Discussion in 'Multiplayer' started by saharrsh, May 6, 2023.

  1. saharrsh

    saharrsh

    Joined:
    Jun 20, 2021
    Posts:
    2
    I'm trying to sync a Line Renderer which is given out by a Grappling Gun and it doesn't seem to be working. I've seen other forums, answers, etc but I'm just stuck with this for the past 2 days.

    Here is the code which I'm using:


    Code (CSharp):
    1. //The Script used to draw the Grappling Rope!
    2.  
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using Photon.Pun;
    6. using UnityEngine;
    7.  
    8. public class GrapplingRope : MonoBehaviour
    9. {
    10.     //References
    11.     [Header("References")]
    12.     [SerializeField] private GrapplingGun grapplingGun;
    13.     [SerializeField] private AnimationCurve affectCurve;
    14.     [SerializeField] private PhotonView photonView;
    15.  
    16.     [Header("Settings")]
    17.     [SerializeField] private int quality;
    18.     [SerializeField] private float damper;
    19.     [SerializeField] private float strength;
    20.     [SerializeField] private float velocity;
    21.     [SerializeField] private float waveCount;
    22.     [SerializeField] private float waveHeight;
    23.  
    24.     private Spring spring;
    25.     private LineRenderer lr;
    26.     private Vector3 currentGrapplePosition;
    27.  
    28.     private bool calledOnlyOnce = false;
    29.  
    30.     [HideInInspector] public bool viewIsMine;
    31.  
    32.     private void Awake()
    33.     {
    34.         if (photonView.IsMine)
    35.         {
    36.             lr = GetComponent<LineRenderer>(); //Get the Line Renderer component
    37.             spring = new Spring(); //Create the Spring component
    38.             spring.SetTarget(0); //Set the Spring target to 0
    39.         }
    40.     }
    41.  
    42.     //Method called after Update Method
    43.     private void LateUpdate()
    44.     {
    45.         if (viewIsMine)
    46.         {
    47.             photonView.RPC("DrawRope", RpcTarget.AllBuffered);
    48.         }
    49.     }
    50.  
    51.     [PunRPC]
    52.     private void DrawRope()
    53.     {
    54.         if (!calledOnlyOnce)
    55.         {
    56.             spring = new Spring(); //Create the Spring component
    57.             spring.SetTarget(0); //Set the Spring target to 0
    58.             lr = GetComponent<LineRenderer>(); //Get the Line Renderer component
    59.  
    60.             calledOnlyOnce = true;
    61.         }
    62.  
    63.         if (!grapplingGun.IsGrappling()) //If not "grappling"
    64.         {
    65.             currentGrapplePosition = grapplingGun.gunTip.position; //Set the grappling position
    66.             spring.Reset(); //Set the spring's velocity and value to 0
    67.  
    68.             if (lr.positionCount > 0) //If line renderer's position is greater than 0
    69.             {
    70.                 lr.positionCount = 0; //Set the  line renderer's position to 0
    71.             }
    72.             return; //Return
    73.         }
    74.  
    75.         if (lr.positionCount == 0) //If line renderer's position is equal to 0
    76.         {
    77.             spring.SetVelocity(velocity); //Set the velocity value
    78.             lr.positionCount = quality + 1; //Set the position count value
    79.         }
    80.  
    81.      
    82.         spring.SetDamper(damper); //Set the damper value
    83.         spring.SetStrength(strength); //Set the strength value
    84.         spring.Update(Time.deltaTime); //Update the spring according to the time
    85.      
    86.         var grapplePoint = grapplingGun.GetGrapplePoint();
    87.         var gunTipPosition = grapplingGun.gunTip.position;
    88.         var up = Quaternion.LookRotation((grapplePoint - gunTipPosition).normalized) * Vector3.up;
    89.  
    90.         currentGrapplePosition = Vector3.Lerp(currentGrapplePosition, grapplePoint, Time.deltaTime * 12f);
    91.  
    92.         for (var i = 0; i < quality + 1; i++)
    93.         {
    94.             var delta = i / (float)quality;
    95.             var offset = up * waveHeight * Mathf.Sin(delta * waveCount * Mathf.PI) * spring.Value *
    96.                          affectCurve.Evaluate(delta);
    97.  
    98.             lr.SetPosition(i, Vector3.Lerp(gunTipPosition, currentGrapplePosition, delta) + offset);
    99.         }
    100.     }
    101. }

    And here is a video explaining the problem:



    I think I'm missing something very basic here so any help would be appreciated!