Make Function run on every Client (in this case a bullet being shot on every client)

PHOTO EMBED

Tue Apr 12 2022 02:10:57 GMT+0000 (Coordinated Universal Time)

Saved by @miguel9177 #c#

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

namespace PhotonTutorial.Weapons
{
    public class Weapon : MonoBehaviourPun
    {
        //this will store how fast the bullet moves
        [SerializeField] private float projectileSpeed = 5f;
        //this will store the projectile that is going to be shooted from the gun
        [SerializeField] private GameObject projectile = null;
        //this will store where the bullets will spawn
        [SerializeField] private Transform spawnPoint = null;

        private void Update()
        {
            //if this is my photon view (if this is my character and not any other players character) call the function that thakes input
            if (photonView.IsMine)
            {
                //call the function that handles the input
                TakeInput();
            }
        }

        private void TakeInput()
        {
            //if the player hasnt clicked mouse button, leave this function
            if(!Input.GetMouseButtonDown(0)) { return; }
            //this will call the function FireProjectile, on every pc on the server
            photonView.RPC("FireProjectile", RpcTarget.All);
        }

        //this will run the code on the function bellow run on every computer of the server (inm this case we want to spawn this bullet in every pc, and move it accordingly
        [PunRPC]
        private void FireProjectile()
        {
            //this is going to spawn the bullet on every computer (since we are using PunRPC)
            GameObject projectileInstance = Instantiate(projectile, spawnPoint.position, spawnPoint.rotation);
            //this is going to add velocity to where the spawn bullet is facing, in this case is from the spawn point
            projectileInstance.GetComponent<Rigidbody>().velocity = projectileInstance.transform.forward * projectileSpeed;
        }
    }
}
content_copyCOPY

This is taken from this video : https://www.youtube.com/watch?v=5QuCtbaI-ag This script fires a bullet online on every client, this has a problem with the smoothing of the multiplayer server, this code is better used for things not shooting related.

https://www.youtube.com/watch?v=5QuCtbaI-ag