Hi! Im making fps game. And i don’t know to sync sounds to everyone. Like shooting etc. I mean if you play only you can hear your own shooting sounds. Here is the weapon code i made.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
using TMPro;
public class Weapon2Script : MonoBehaviour
{
public GameObject Sound;
public int damage;
public Camera camera;
public float fireRate;
private float nextFire;
public GameObject hitVfx;
public int mag = 60;
public int ammo = 5;
public int magammo = 5;
public TextMeshProUGUI magText;
public TextMeshProUGUI ammoText;
public Animation animation;
public AnimationClip Reload2;
void Start()
{
magText.text = mag.ToString();
ammoText.text = ammo + "/" + magammo;
}
void Update()
{
if (nextFire > 0)
{
nextFire -= Time.deltaTime;
}
if (Input.GetButton("Fire1") && nextFire <= 0 && ammo > 0 && animation.isPlaying == false)
{
nextFire = 5 / fireRate;
ammo--;
magText.text = mag.ToString();
ammoText.text = ammo + "/" + magammo;
Fire();
}
if (Input.GetKeyDown(KeyCode.R))
{
Reload();
}
}
void Reload()
{
if (mag > 0)
{
mag--;
animation.Play(Reload2.name);
ammo = magammo;
}
magText.text = mag.ToString();
ammoText.text = ammo + "/" + magammo;
}
void Fire()
{
Instantiate(Sound);
Ray ray = new Ray(origin: camera.transform.position, direction: camera.transform.forward);
RaycastHit hit;
if (Physics.Raycast(ray.origin, ray.direction, out hit, maxDistance: 100f))
{
PhotonNetwork.Instantiate(hitVfx.name, hit.point, Quaternion.identity);
if (hit.transform.gameObject.GetComponent<Health>())
{
hit.transform.gameObject.GetComponent<PhotonView>().RPC(methodName: "TakeDamage", RpcTarget.All, damage);
}
}
}
}