You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity PUN 2中Line Renderer无法被其他玩家看见的问题求助

抓钩枪Line Renderer同步问题求助

我尝试同步抓钩枪生成的Line Renderer,但始终无法正常工作,已经为此卡了两天,查阅了诸多论坛及相关解决方案都没有解决。以下是我使用的代码:

//The Script used to draw the Grappling Rope!

using System.Collections;
using System.Collections.Generic;
using Photon.Pun;
using UnityEngine;

public class GrapplingRope : MonoBehaviour
{
    //References
    [Header("References")]
    [SerializeField] private GrapplingGun grapplingGun;
    [SerializeField] private AnimationCurve affectCurve;
    [SerializeField] private PhotonView photonView;
    
    [Header("Settings")]
    [SerializeField] private int quality;
    [SerializeField] private float damper;
    [SerializeField] private float strength;
    [SerializeField] private float velocity;
    [SerializeField] private float waveCount;
    [SerializeField] private float waveHeight;

    private Spring spring;
    private LineRenderer lr;
    private Vector3 currentGrapplePosition;

    private bool calledOnlyOnce = false;
    
    [HideInInspector] public bool viewIsMine;

    private void Awake()
    {
        if (photonView.IsMine)
        {
            lr = GetComponent<LineRenderer>(); //Get the Line Renderer component
            spring = new Spring(); //Create the Spring component
            spring.SetTarget(0); //Set the Spring target to 0   
        }
    }

    //Method called after Update Method
    private void LateUpdate()
    {
        if (viewIsMine)
        {
            photonView.RPC("DrawRope", RpcTarget.AllBuffered);
        }
    }
    
    [PunRPC]
    private void DrawRope()
    {
        if (!calledOnlyOnce)
        {
            spring = new Spring(); //Create the Spring component
            spring.SetTarget(0); //Set the Spring target to 0
            lr = GetComponent<LineRenderer>(); //Get the Line Renderer component

            calledOnlyOnce = true;
        }

        if (!grapplingGun.IsGrappling()) //If not "grappling"
        {
            currentGrapplePosition = grapplingGun.gunTip.position; //Set the grappling position
            spring.Reset(); //Set the spring's velocity and value to 0

            if (lr.positionCount > 0) //If line renderer's position is greater than 0
            {
                lr.positionCount = 0; //Set the  line renderer's position to 0
            }
            return; //Return
        }

        if (lr.positionCount == 0) //If line renderer's position is equal to 0
        {
            spring.SetVelocity(velocity); //Set the velocity value
            lr.positionCount = quality + 1; //Set the position count value   
        }

        
        spring.SetDamper(damper); //Set the damper value
        spring.SetStrength(strength); //Set the strength value
        spring.Update(Time.deltaTime); //Update the spring according to the time
        
        var grapplePoint = grapplingGun.GetGrapplePoint();
        var gunTipPosition = grapplingGun.gunTip.position;
        var up = Quaternion.LookRotation((grapplePoint - gunTipPosition).normalized) * Vector3.up;

        currentGrapplePosition = Vector3.Lerp(currentGrapplePosition, grapplePoint, Time.deltaTime * 12f);

        for (var i = 0; i < quality + 1; i++)
        {
            var delta = i / (float)quality;
            var offset = up * waveHeight * Mathf.Sin(delta * waveCount * Mathf.PI) * spring.Value *
                         affectCurve.Evaluate(delta);

            lr.SetPosition(i, Vector3.Lerp(gunTipPosition, currentGrapplePosition, delta) + offset);
        }
    }
}

另有视频演示该问题,我认为自己遗漏了一些基础内容,希望能得到相关帮助!

内容的提问来源于stack exchange,提问作者Saharsh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 23:03:31