- Home /
Proper way to instantiate projectile in PUN
I've been fighting with Photon Unity Networking for a few days now with trying to instantiate my rocket projectile properly. I've been told by Tobias on the Exit Games forums that using PhotonNetwork.Instantiate is completely unnecessary (good thing, too... because it didn't do anything) and that it's best to just use an RPC call. I've tried that with this code:
void fireRocket(Vector3 bottleRocketStart, Quaternion bottleRocketRot)
{
GetComponent<PhotonView>().RPC("fireRocket_RPC", PhotonTargets.MasterClient, transform.position, Quaternion.identity, nm.teamID);
GetComponent<GetItem>().currentItem = "";
}
[RPC]
void fireRocket_RPC(Vector3 bottleRocketStart, Quaternion bottleRocketRot, int teamID)
{
bottleRocket = (GameObject)Instantiate(Resources.Load("BottleRocketProjectile"), mouseWeaponOrigin.transform.position, Camera.main.transform.rotation);
bottleRocket.GetComponent<RocketInfo>().shotByPlayer = teamID;
}
This successfully spawns the rocket. However, I run into issues when trying to destroy the rocket in this script, which is attached to the rocket prefab.
void OnCollisionEnter(Collision col)
{
if (col.gameObject.name != "BottleRocketProjectile(Clone)" && col.gameObject.name != PhotonNetwork.playerName) {
GetComponent<PhotonView>().RPC("spawnExplosion", PhotonTargets.MasterClient);
GetComponent<PhotonView>().RPC("DestroyOnNetwork", PhotonTargets.MasterClient);
}
}
[RPC]
void spawnExplosion()
{
GameObject bottleRocketSplat = (GameObject)Instantiate(Resources.Load("BottleRocketSplat"), transform.position, transform.rotation);
}
[RPC]
public void DestroyOnNetwork()
{
Destroy(gameObject);
}
The explosion is spawned, but the rocket is not destroyed. I get errors here complaining about viewIDs... which makes sense, since I'm not PhotonNetwork.Instantiating them. That's where I'm losing track of what I'm supposed to be doing. If I'm not supposed to instantiate them on the network, how do I properly call the RPCs to handle the destruction of the rocket and instantiation of the explosion?
These are the errors I get on collision with something
Illegal view ID:0 method: spawnExplosion GO:BottleRocketProjectile(Clone)
UnityEngine.Debug:LogError(Object)
NetworkingPeer:RPC(PhotonView, String, PhotonTargets, Boolean, Object[]) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/NetworkingPeer.cs:2915)
PhotonNetwork:RPC(PhotonView, String, PhotonTargets, Boolean, Object[]) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonNetwork.cs:2496)
PhotonView:RpcSecure(String, PhotonTargets, Boolean, Object[]) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonView.cs:589)
PhotonView:RPC(String, PhotonTargets, Object[]) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonView.cs:557)
RocketInfo:OnCollisionEnter(Collision) (at Assets/RocketInfo.cs:24)
Illegal view ID:0 method: DestroyOnNetwork GO:BottleRocketProjectile(Clone)
UnityEngine.Debug:LogError(Object)
NetworkingPeer:RPC(PhotonView, String, PhotonTargets, Boolean, Object[]) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/NetworkingPeer.cs:2915)
PhotonNetwork:RPC(PhotonView, String, PhotonTargets, Boolean, Object[]) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonNetwork.cs:2496)
PhotonView:RpcSecure(String, PhotonTargets, Boolean, Object[]) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonView.cs:589)
PhotonView:RPC(String, PhotonTargets, Object[]) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonView.cs:557)
RocketInfo:OnCollisionEnter(Collision) (at Assets/RocketInfo.cs:25)
PhotonView with ID 0 has no method "DestroyOnNetwork" marked with the [RPC](C#) or @RPC(JS) property! Args:
UnityEngine.Debug:LogError(Object)
NetworkingPeer:ExecuteRPC(Hashtable, PhotonPlayer) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/NetworkingPeer.cs:2190)
NetworkingPeer:RPC(PhotonView, String, PhotonTargets, Boolean, Object[]) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/NetworkingPeer.cs:2979)
PhotonNetwork:RPC(PhotonView, String, PhotonTargets, Boolean, Object[]) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonNetwork.cs:2496)
PhotonView:RpcSecure(String, PhotonTargets, Boolean, Object[]) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonView.cs:589)
PhotonView:RPC(String, PhotonTargets, Object[]) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonView.cs:557)
RocketInfo:OnCollisionEnter(Collision) (at Assets/RocketInfo.cs:25)
Answer by 334499p · Jul 19, 2015 at 01:23 AM
Since the rocket is technically client-based, you would need to destroy it on all clients, not just the Master Client. If the rocket were instantiated using the photon network then you would need to use PhotonNetwork.Destroy(PhotonView), but this isn't the case.
-Errors: -Only instantiating the rocket on the master clients... you need to call PhotonTargets.All instead of PhotonTargets.MasterClient for the first script and make sure there is a PhotonView attached to the object that contains that script.
-Calling an RPC for the explosion of the rocket... You don't need to call an RPC since all rockets are client based. Say you have 10 players on the server and one person shoots a rocket; that means that the one person who shoots the rocket tells all players including themselves through an RPC to spawn a rocket that isn't actually synced across the network. When each client spawns this rocket it will follow its own script, so you need to only have the OnCollisionEnter and the proceeding explosion and Destroy () method for the second script with no PhotonView attached the rocket prefab.
Using this method however is not ideal if you actually want to tie operations to the explosion of the rocket such as hurting other players. A different method would be to use two different rockets and their own scripts. One rocket would belong to the player(and could only be seen by that player) and would cover all operations such as damage.The other rocket prefab would have it's own script to simply explode on collision and nothing else for the other clients. One RPC would execute the spawning of projectiles for PhotonTargets.Others and then you would spawn you're own rocket. Neither rocket prefab would need a PhotonView or RPCs to die as mentioned above.
Your answer
Follow this Question
Related Questions
Photon Network Instantiate Objects over Network 1 Answer
Photon Networking Instantiating Problem 0 Answers
Objects instantiated by player owned object aren't owned by the player? 2 Answers
Photon RPC methodcall 1 Answer
RPC with Random Numbers! 2 Answers