I’m trying to PunRPC (send updates in-game with photon to all other players) the weapons changes/upgrades that happen during gameplay, but I’m missing something:
[PunRPC]
void OnTriggerEnter(Collider other)
{
if (photonView.isMine)
{
if (other.tag == "BeeSting")
{
beePrefab.SetActive(true);
}
In my code above, the “beePrefab” (a missile that is a child object on each player) becomes active and should be visible to all other players, when the player smashes the “BeeSting” crate (OnTriggerEnter)
But it doesn’t work and I know I’m missing something as it is not enough to write [PunRPC] above the function. How do I show all other players these weapons updates in-game?
Off the top of my head it needs to be something more like this…
void OnTriggerEnter(Collider other)
{
if (photonView.IsMine)
{
if (other.tag == "BeeSting")
{
ShowBee(); // Show the prefab on this client
photonView.RPC("ShowBee", RpcTarget.OthersBuffered); // This will execute the ShowBee function on remote clients already in the game and new clients when they join (because it is buffered).
}
}
}
[PunRPC]
void ShowBee()
{
beePrefab.SetActive(true);
}
Please note, this syntax is for PUN2 and will generate compile errors with PUN1 (which I suspect you may be using due to the lowercase i on the PhotonView.isMine in your code).
If you’re just starting out with your project, I’d strongly recommend you change to PUN2.
Thank you so much for your help it works now with the following code: photonView.RPC("ShowBee", PhotonTargets.All); It doesn’t need to update all buffered because nobody can join once a deathmatch has been started
@Munchy2007 I’m having a problem with the missiles movement not updating on all other players screens and I noticed another one of your posts where you wrote that you RPC the missile’s movement in the projectiles movement script. This is my script. Do you know how to RPC it’s transform.Position, transform.Rotation?
public Rigidbody rocketRigidBody;
public float rocketVelocity;
public GameObject explosionAW;
public GameObject explosionUW;
public GameObject smokeTrail;
//public GameObject particles;
public Collider coll;
public GameObject Bee;
public MeshRenderer body;
public GameObject beeStingIcon;
float currentSpeed;
//public SelfDestruct s;
IEnumerator CollOn()
{
yield return new WaitForSeconds(.1f);
coll.enabled = true;
}
IEnumerator MouseDownDelay()
{
yield return new WaitForSeconds(.1f);
rocketRigidBody.isKinematic = false;
StartCoroutine(CollOn());
Bee.transform.parent = null;
rocketRigidBody.velocity = transform.forward * rocketVelocity;
}
void Start () {
explosionAW.SetActive(false);
explosionUW.SetActive(false);
smokeTrail.SetActive(false);
rocketRigidBody = GetComponent<Rigidbody>();
rocketRigidBody.isKinematic = true;
body.enabled = true;
}
void FixedUpdate()
{
//if (photonView.isMine)
//{
if (Input.GetMouseButtonDown(0))
{
StartCoroutine(MouseDownDelay());
smokeTrail.SetActive(true);
beeStingIcon.SetActive(false);
}
//}
}
void OnCollisionEnter(Collision collision)
{
if (rocketRigidBody.transform.position.y > 0)
{
explosionAW.SetActive(true);
explosionUW.SetActive(false);
body.enabled = false;
smokeTrail.SetActive(false);
//particles.SetActive(true);
rocketRigidBody.isKinematic = true;
StartCoroutine(MissileDestroy());
}
if (rocketRigidBody.transform.position.y < 0)
{
explosionUW.SetActive(true);
explosionAW.SetActive(false);
body.enabled = false;
smokeTrail.SetActive(false);
//particles.SetActive(true);
rocketRigidBody.isKinematic = true;
StartCoroutine(MissileDestroy());
}
}
IEnumerator MissileDestroy()
{
yield return new WaitForSeconds(4f);
Destroy(gameObject);
}
}
The coroutine has the transform forward velocity in it and it’s activated in Fixed Update if (Input.GetMouseButtonDown(0))