- Home /
Changing Parenting through PhotonNetwork???
ok, i want to create a breath of fire that comes out of the player, im using a particle effect that is being instantiated through the network using, photonnetwork.instantiate. it works perfectly fine. in the same "if" statement, i have a code that parents the prefab "flamebaby" (which has an ongoing particle system attatched to it) with the camera so that the flame follows wherever you look.
problem is, when i press "F", you can catch a glimpse of fire come out from where its suppose to but then move to the location that the Prefab is in when its NOT parented.
i guess what im asking is, how do i parent objects over the network so that all clients can see the fire from where it should come from.
i hope that made sense, here is my code.
using UnityEngine;
using System.Collections;
public class Camerafire : Photon.MonoBehaviour {
Camera cam = Camera.main;
GameObject fire;
GameObject myGuy;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
myGuy = GameObject.Find ("Cameralol(Clone)");
if (Input.GetKeyDown (KeyCode.F )) {
fire = (GameObject)PhotonNetwork.Instantiate("flamebaby", transform.position + transform.forward , transform.rotation, 0);
//this is the part that is giving me trouble.
fire.transform.parent = myGuy.transform;
}
if (Input.GetKeyUp (KeyCode.F)) {
PhotonNetwork.Destroy (fire);
}
}
}
Answer by FLASHDENMARK · Oct 17, 2013 at 08:21 PM
Use RPC calls instead of PhotonNetwork.Instantiate();:
using UnityEngine;
using System.Collections;
public class Camerafire : Photon.MonoBehaviour {
Camera cam = Camera.main;
GameObject fire;
GameObject myGuy;
GameObject flameBaby; //Drag "flamebaby" here in the Inspector
// Use this for initialization
void Start () {
//Also DO NOT use GameObject.Find(or similar) every update call. It is expensive!
//Just call it once
myGuy = GameObject.Find ("Cameralol(Clone)");
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown (KeyCode.F )) {
photonView.RPC("InstantiateFlame", PhotonTargets.All);
}
if (Input.GetKeyUp (KeyCode.F)) {
photonView.RPC("DestroyFlame ", PhotonTargets.All);
}
}
[RPC] void InstantiateFlame (){
fire = (GameObject)Instantiate(flameBaby, transform.position + transform.forward , transform.rotation);
//this is the part that is giving me trouble.
fire.transform.parent = myGuy.transform;
}
[RPC] void DestroyFlame (){
if(fire){
Destroy(fire);
}
}
}
When you use PhotonNetwork.Instantiate(or Network.Instantiate for build-in networking) you are given much less control of the object you are Instantiating. When using RPCs(photonView.RPC("")) you can do whatever you want to do with that object within the function you are calling.
What you have to do is to tell all other players to parent the "flamebaby" to the "myGuy" transform. And in this situation the simplest, easiest and cleanest way of doing so is through sole RPC calls.
awesome bro! thanks, but only problem is, it says "method must have a return type" on [RPC] InstantiateFlame, and [RPC] DestroyFlame. do i just add a "Void"?
oh man, wait bro, fire = (GameObject)Instantiate("flamebaby", transform.position + transform.forward , transform.rotation);
the "flamebaby" string cant be a string it gives me a -- cannot convert from string to unityengine.gameobject ---
Whoops. Yes you just add a "void", my bad.
And when using "regular" Instantiate it does not accept a string as the first parameter like PhotonNetwork.Instantiate does. I have updated the answer.
Also fire.transform.parent = myGuy.transform; Change to fire.transform.SetParent(myGuy.transform);
Answer by Real_Person_ · Oct 16, 2021 at 12:06 PM
@FLASHDENMARK This is interesting but I'm having a slightly different problem. What if you want to change the parent of an object that was not instantiated. i.e exists in the scene before playing the game. How would you do this with Photon and allow the transformation of the new child to be updated across clients with it only being the child of one of the client's players?
Hope this makes sence? Thanks
Your answer
Follow this Question
Related Questions
parenting a camera with a sphere 3 Answers
Problem with Ammo Pickup 1 Answer
Unexpected behavior between parent/child rigidbodies 0 Answers
Make instantiation follow me. 1 Answer
Creating a empty object to not lose children's scale 1 Answer