【问题标题】:RPC in photon not sending data over the network光子中的 RPC 不通过网络发送数据
【发布时间】:2021-09-26 02:40:03
【问题描述】:

嗯,所以我尝试使用光子 RPC,但它似乎没有通过网络发送信息?

目前,当玩家射击时,它只能在他们的 PC 上显示,但在同一个房间里玩的其他人看不到它

PlayerShoot.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
public class playerShoot : MonoBehaviourPunCallbacks
{
    public Transform shootPoint;
    public LineRenderer gunLine;
    public float range = 30f;
    RaycastHit hit;
    Ray ray;
    public PhotonView photonview;
    // Start is called before the first frame update
    void Start()
    {
        gunLine = GetComponent<LineRenderer>();
        photonview = PhotonView.Get(this);
        
    }

    // Update is called once per frame
    void Update()
    {
        if (!photonView.IsMine)
            return;
        photonview.RPC("shoot",RpcTarget.All);
        //shoot();
    }
    [PunRPC]
    public void shoot()
    {
        if (!photonView.IsMine)// still need to call this coz the one in update doesn't seem to work?
            return;

        if (Input.GetButton("Fire1"))
        {
            gunLine.enabled = true;
            gunLine.SetPosition(0, shootPoint.position);

            ray.origin = shootPoint.position;
            ray.direction = transform.forward;
            if (Physics.Raycast(ray, out hit, range))
            {

                //Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * hit.distance, Color.red);
                Debug.Log(hit.point);
                gunLine.SetPosition(1, hit.point);
            }
            else
            {
                gunLine.SetPosition(1, ray.origin + ray.direction * range);
            }

        }
        else
            gunLine.enabled = false;
    }
}

显示问题的视频:

https://youtu.be/RMtCDTCyQk0

【问题讨论】:

    标签: c# visual-studio unity3d photon


    【解决方案1】:

    在所有接收器上,if (!photonView.IsMine) 将为 false,因此那里不会发生任何事情。

    您也确实想从接收器获取键盘/鼠标输入,而是从本地设备获取!此外,Raycast 只能在本地设备上进行。

    然后,一旦您收集了所有必需的值,您只需将这些值传递给拍摄方法和其他所有人。

    它应该看起来像例如

    void Update()
    {
        if (!photonView.IsMine)
            return;
    
        // Gather all this information only on the local device
        var firePressed = Input.GetButton("Fire1");
        var position0 = shootPoint.position;
        var position1 = Vector3.zero;
    
        if (firePressed)
        {
            ray.origin = shootPoint.position;
            ray.direction = transform.forward;
            if (Physics.Raycast(ray, out hit, range))
            {
                Debug.Log(hit.point);
                position1 = hit.point;
            }
            else
            {
                position1 = ray.origin + ray.direction * range;
            }
        }
    
        // Then forward it to the actual shoot and to all other clients
        photonview.RPC("shoot",RpcTarget.All, firePressed, position0, position1);
    }
    
    [PunRPC]
    public void shoot(bool firePressed, Vector3 position0, Vector3 position1)
    {
        // Now having all required values every device can reproduce the desired behavior
        gunLine.enabled = firePressed;
    
        if(firePressed)
        {
            gunLine.SetPosition(0, position0);
            gunLine.SetPosition(1, position1);
        }  
    }
    

    但是,请注意每帧发送一个 RPC 调用是非常糟糕的。您可能会考虑让您的拍摄以特定的时间间隔进行(至少对于远程客户端而言)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-29
      • 2011-10-16
      • 1970-01-01
      相关资源
      最近更新 更多