How do you Instantiate particles using Photon?
I'm trying to make sure everyone can see each other's spells when they cast them using PhotonView (hint: I have no idea how anything in Photon works) What do I have to do to get my current script to work online?
using UnityEngine;
using Valve.VR;
public class Wizard_Spell : MonoBehaviour
{
public GameObject Spell1;
public SteamVR_Action_Boolean TriggerClick;
public SteamVR_Input_Sources inputSource1;
private void OnEnable()
{
TriggerClick.AddOnStateDownListener(CastSpell, inputSource1);
}
private void OnDisable()
{
TriggerClick.RemoveOnStateDownListener(CastSpell, inputSource1);
}
private void CastSpell(SteamVR_Action_Boolean fromAction, SteamVR_Input_Sources fromSource)
{
Instantiate(Spell1, transform.position, transform.rotation);
}
}
Answer by Captain_Pineapple · May 10, 2019 at 08:09 AM
Hey there,
before you start on this please read the documentations on photon. the most important things for you will be Remove Procedure Calls, the general pun photon view doku and how to create and join rooms
Do this before you continue working on this!
When you think you understood the basics then add a photon view to your current player object. This will be the component that manages incoming and outgoing messages to this object. If you have an object that you want to sync in position, ad a photon transform view. Only use custom serialization code if you really have to. In general i'd suggest to keep the number of photon views as small as possible. For this reason i'd take your current WizardSpell script and make it call a function on your main character behaviour to use that photon view for "networked spellcasting".
In connection to this i'd suggest you read into Object Pooling. This will create a static class for you from which you can request Object Instances. If you assign an ID to each of your spells you can easily create them without any performance issues.
For now let's assume that you add object pooling later on and for now just have some public static GameObject[] allSpellPrefabs;
allSpellPrefabs
will have all spells as prefabs in one array and since its public static you can access it from anywhere. Then you can use the following function to cast spells over the network:
[PunRPC]
public void castSpell (int index)
{
//this checks if this object instance belongs to the local player:
if(photonView.IsMine)
{
//call this method for all other players in the current room:
photonView.RPC("castSpell", PhotonTargets.Others, index);
}
//instantiate the requested spell by index. Get an object
Instantiate(Spell1, transform.position, transform.rotation);
}
(so to call this replace the instanciation call on your wizardspell script by some means to find you local player object whatever it may be called and call the castspell function on it.)
Hope this helps and you did not drown in new knowledge. If something needs further clarification let me know and i will try to elaborate a bit more on it.