Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Third Party Photon RPC now sending data over network

Discussion in 'Multiplayer' started by eatitall2002, Jul 17, 2021.

  1. eatitall2002

    eatitall2002

    Joined:
    Apr 25, 2021
    Posts:
    7
    Umm so I was trying to use photon RPCs but it doesn't seem to send information over the network?
    PlayerShoot.cs
    ```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:
     
  2. u-doug

    u-doug

    Joined:
    Jun 6, 2017
    Posts:
    11
    Assuming you are trying to draw the line on both clients;

    You are calling the RPC correctly, but in the RPC itself (which is executed on all clients) you are again checking IsMine and for input. When the remote client executes that RPC they will just return, because it is being called on a PhotonView which does not belong to them.

    Check for IsMine / Input in Update(), and if true call an RPC which draws the debug line separately with no IsMine / Input checks.
     
  3. eatitall2002

    eatitall2002

    Joined:
    Apr 25, 2021
    Posts:
    7
    Ohh Thanks