Search code examples
c#unity-game-engineaugmented-realityarkit

Need clarification of one thing in this AR Multiplayer Tutorial


I'm experimenting with the simple AR multiplayer app, made using this tutorial:

https://www.youtube.com/watch?v=n3a-aaSYR8s

SourceCode

It works! But I'm not sure, why 2 objects which were instantiated the same way positioned differently: The moon and the player.

Why does the "Player" game object remain attached to the user's phone, while the "Moon" remains attached to some location in the room? And why the location of the moon after the instantiation is not the same as the Player?

Both of them are instantiated with the same command:

PhotonNetwork.Instantiate ("SampleMoon", Vector3.zero, Quaternion.identity, 0);

If the instantiating code is the same for both of them, does the difference in positioning has something to do with those prefabs itself? What exactly causes it?


Solution

  • Have a look again on the video from 17:16!

    The difference is not in the instanciation but rather in the components the instantiated objects (prefabs) have attached:

    MoonController (used by the Moon) vs PlayerController (used by the Player)


    While the MoonController basically does nothing than registering the Moon:

    void Start () 
    {
        // Register this object to the current game controller.
        // This is important so that all clients have a reference to this object.
        GameController.Instance.RegisterMoon (this);
    }
    

    The PlayerController gets always set to the position and rotation of the camera in the Update method (in this app "Camera = Players Smartphone" ). I marked the significant lines bold:

    public Transform CameraTransform;
    
        private void Start ()
        {
            CameraTransform = FindObjectOfType ().transform;
            // Register this player to the GameController. 
            // Important: all clients must have a reference to this player.
            GameController.Instance.RegisterPlayer (this);
    
            // Hide your own model if you are the local client.
            if (photonView.isMine)
                gameObject.transform.GetChild (0).gameObject.SetActive (false);
        }
    
        void Update () 
        {
            // If this player is not being controlled by the local client
            // then do not update its position. Each client is responsible to update
            // its own player.
            if (!photonView.isMine && PhotonNetwork.connected)
                return;
    
            // The player should have the same transform as the camera
            gameObject.transform.position = CameraTransform.position;
            gameObject.transform.rotation = CameraTransform.rotation;
        }
    }

    So in the first frame after instantiating both objects, the Player object is already on the position and rotation of the Camera, while the Moon remains on it's instantiation point 0,0,0.