简体   繁体   中英

RPC in photon not sending data over the network

Umm so I was trying to use photon RPCs but it doesn't seem to send information over the network?

Currently when a player shoots it's show only on their PC but anyone else playing on the same room can't see it

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;
    }
}

A video showing the problem:

https://youtu.be/RMtCDTCyQk0

On all receivers the if (!photonView.IsMine) will be false so nothing will happen there.

You also do not want to get the keyboard/mouse input from the receivers but rather the local device! Also the Raycast should only be done on the local device.

Then once you have gathered all required values you pass only these on to the shoot method and to everyone else.

It should rather look like eg

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);
    }  
}

However , note that it is quite bad to send an RPC call every frame . You might consider to rather have your shoot happening in certain intervals (at least for the remote clients).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM