How to run a RPC Coroutine?
The whole answer can be found below.
Try Our
Documentation
Please check if you can find an answer in our extensive documentation on PUN.
Join Us
on Discord
Meet and talk to our staff and the entire Photon-Community via Discord.
Read More on
Stack Overflow
Find more information on Stack Overflow (for Circle members only).
How to run a RPC Coroutine?
JXTQ
2021-07-22 13:29:11
Hi, I am trying to make a bullet script where 2 sounds play after the other after a certain amount of time after the bullet is shot so than all players can hear it but it seems like when I do photonView.RPC("Coroutine", RpcTarget.All) it does not work because it is not starting the coroutine and it just spams my console when it gets to the yield return new WaitForSeconds()
here is my code
void Update()
{
if (photonView.IsMine)
{
if (Input.GetKey(KeyCode.Mouse1))
{
scope.SetActive(true);
crosshair.SetActive(false);
if (Input.GetKeyDown(KeyCode.Mouse0) && !isPlaying)
{
isPlaying = true;
photonView.RPC("RPC_PlaySound", RpcTarget.All);
}
}
else
{
scope.SetActive(false);
crosshair.SetActive(true);
}
}
}
[PunRPC]
IEnumerator RPC_PlaySound()
{
AudioSource sound = gun.transform.Find("Sound1").GetComponent<AudioSource>();
AudioSource sound2 = gun.transform.Find("Sound2").GetComponent<AudioSource>();
flashEffect.Play();
photonView.RPC("RPC_Fire", RpcTarget.All);
sound.Play();
yield return new WaitForSeconds(2);
sound2.Play();
yield return new WaitForSeconds(0.5f);
isPlaying = false;
}
[PunRPC]
public void RPC_Fire()
{
bullet = Instantiate(bulletPrefab);
bullet.transform.position = bulletSpawn.position;
Vector3 rotation = bullet.transform.rotation.eulerAngles;
bullet.transform.rotation = Quaternion.Euler(rotation.x, transform.eulerAngles.y, rotation.z);
bullet.GetComponent<Rigidbody>().AddForce(bulletSpawn.forward * bulletSpeed, ForceMode.Impulse);
StartCoroutine(DestroyBulletAfterTime(bullet, lifeTime));
}
IEnumerator DestroyBulletAfterTime(GameObject bullet, float delay)
{
yield return new WaitForSeconds(delay);
Destroy(bullet);
}
This has been making me pull my hair out for ages and I am unsure how this would work
Comments
Probably it is not starting coroutine, cause u didn't call StartCoroutine for RPC_PlaySound. Does it work for DestroyBulletAfterTime ?
Back to top