Search Unity

  1. We have added dedicated sub-forums for UNet, the experimental MLAPI and Unity Transport Package. Please use the new forums to discuss related topics.
    Dismiss Notice
  2. How can our website serve you better? Give us your feedback. Take our survey and let us know.
    Dismiss Notice

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
     
unityunity