PhotonNetwork.Instantiate I don't understand?

Hi.

(I'm using Photon Cloud)
I'm trying to Instantiate my character prefab, but it is setup like this:

- EmptyGameObject
-- CameraPivot
--- MainCamera
-- Character

2ug093a.jpg

Now i can't get it to work correctly. The controller scripts is attached to "Character" and the camera scripts is attached
to the "MainCamera" and "CameraPivot". When i Instantiate this prefab, where should the photonview be attached to?
And what should i do about the maincamera because there can't be two maincamera's in a scene, can there?.
Currently i just attached a photonview on "characterprefab" and then only one client can walk around, so that doesn't work.

I use this on the "characterprefab" (EmptyGameObject):
private Vector3 correctPlayerPos = Vector3.zero;
    private Quaternion correctPlayerRot = Quaternion.identity;
	
	CameraPivot cameraScript;

    void Awake()
    {
	    cameraScript = GetComponentInChildren<CameraPivot>();
		
		if (photonView.isMine)
        {
            cameraScript.enabled = true;
        }
        else
        {           
            cameraScript.enabled = false;
        }
		
		
        ThirdPersonController myC = GetComponentInChildren<ThirdPersonController>();
        myC.isControllable = photonView.isMine;
    }

    void Update()
    {
        if (!photonView.isMine)
        {
            transform.position = Vector3.Lerp(transform.position, this.correctPlayerPos, Time.deltaTime * 5);
            transform.rotation = Quaternion.Lerp(transform.rotation, this.correctPlayerRot, Time.deltaTime * 5);
        }
    }

    void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    {
        if (stream.isWriting)
        {
            stream.SendNext(transform.position);
            stream.SendNext(transform.rotation);

            ThirdPersonController myC = GetComponent<ThirdPersonController>();
            stream.SendNext((int)myC._characterState);
        }
        else
        {
            this.correctPlayerPos = (Vector3)stream.ReceiveNext();
            this.correctPlayerRot = (Quaternion)stream.ReceiveNext();

            ThirdPersonController myC = GetComponent<ThirdPersonController>();
            myC._characterState = (CharacterState)stream.ReceiveNext();
        }
    }

And i instantiate the player with this:
player = PhotonNetwork.Instantiate("characterprefab", spawnLocations[randomPickSpawn].position, Quaternion.identity, 0);
ThirdPersonController controller = player.GetComponentInChildren<ThirdPersonController>();
controller.enabled = true;

NOW i know this is allot to chew on. But i just can't get my head around this. So i would really appreciate any help!

Comments

  • ColaCube,

    Did you put your prefab under folder Resources ? If not please do so...
    (i assume you are using Unity 3D, right)
    After you have created the prefab, drag the ThirdPersonCamera, ThirdPersonController and ThirdPersonNetwork script to your prefab.
    Next, add photon view to your prefab and drag the ThirdPersonNetwork(the one just added into prefab ) to observe field of your photon view.
    To add photon view, click on Windows ->Photon Unity Networking ->Add Photon view

    Hope this will be help...
  • xhtai4 wrote:
    ColaCube,

    Did you put your prefab under folder Resources ? If not please do so...
    (i assume you are using Unity 3D, right)
    After you have created the prefab, drag the ThirdPersonCamera, ThirdPersonController and ThirdPersonNetwork script to your prefab.
    Next, add photon view to your prefab and drag the ThirdPersonNetwork(the one just added into prefab ) to observe field of your photon view.
    To add photon view, click on Windows ->Photon Unity Networking ->Add Photon view

    Hope this will be help...

    Okay thank you. But what i don't understand is, like in the viking demo the first client has a MainCamera, but the second client doesn't have a maincamera attached, BUT still has a camera in scene view? i don't get it. How does that work? What camera is the second client using?.
  • Check out the code for ThirdPersonNetworkVik.cs
     //TODO: Bugfix to allow .isMine and .owner from AWAKE!
            if (photonView.isMine)
            {
                //MINE: local player, simply enable the local scripts
                cameraScript.enabled = true;
                controllerScript.enabled = true;
                Camera.main.transform.parent = transform;
                Camera.main.transform.localPosition = new Vector3(0, 2, -10);
                Camera.main.transform.localEulerAngles = new Vector3(10, 0, 0);
    
            }
            else
            {           
                cameraScript.enabled = false;
                controllerScript.enabled = true;
    
            }
    

    If the this is local player then it will enable the camera script...
    (I'm a newbie in Gaming development, so if anything wrong please correct me..... ;) )
Sign In or Register to comment.