PUN Basics Tutorial - repositioning player after leaving the room doesn't work

Hi.
I think I've spotted an error when following the Basic PUN tutorial. I also verified that this problem exists when running the completed demo from Demos folder.

I'm starting two games, one in the Unity's window, one as an executable after building it. I join the same room, two players spawn. I move the player IN THE UNITY WINDOW to the edge of the arena. Now, when I leave the room from the executable game, the player in the Unity window is not repositioned to the (0,5,0) position above the arena and just falls down infinitely.
I first thought that it might be a problem with animator and root motion. However, what is really strange is that when I do it in a reverse manner, moving the player in the executable to the edge of the arena and leaving the room in the Unity window, it seems that the player is repositioned correctly, which leads me to the conclusion that it might be something with the position synchronization?

Thanks!

Comments

  • I finally solved the issue. The problem was in the applyRootMotion and how it interacts with the position of the character.
    If anyone encounters the issue, below is the solution. Also - maybe you would like to include it in the PUN Basics Tutorial, since currently as far as I know it doesn't work correctly.

    First - you cannot manually set the position of the character when the applyRootMotion is enabled, so you need to disable it for a moment when you set a new position.

    Second - you need to wait one full frame before you turn it back again for the changes in position to take place.

    Third - it should be done it LateUpdate (don't know why actually, but found this tip somewhere and it works).

    So the full solution looks something like this:
    private void LateUpdate()
        {
            if (!animator.applyRootMotion) animator.applyRootMotion = true;
    
            if (shouldBeMovedBackToArena)
            {
                animator.applyRootMotion = false;
                transform.position = new Vector3(0f, 10f, 0f);
                shouldBeMovedBackToArena = false;
            }    
        }
    

    And in the OnSceneLoaded method you don't set the position of the character, just set
    shouldBeMovedBackToArena= true;
    

    Hope it will help someone going through similar issues.