Issue in Advanced Tutorial Chapter 3

Options
Hi, I had a problem with Bolt Advanced Tutorial chapter 3 : https://doc.photonengine.com/en-us/bolt/current/demos-and-tutorials/advanced-tutorial/chapter-3

I followed all the steps but in the end, when I try (play as server the Level2), the character is at position (0,0,0) (and then fall infinitely because of gravity) instead of being at the RandomPosition we compute during the spawn.

In TutorialPlayerObject.cs in Spawn method, after this line : character.transform.position = RandomPosition();
I log the character.transform.position and it is a correct random position, however the character is always at (0,0,0).

I also log the transform.localPosition in PlayerMotor.Awake, it is at position (0,0,0) and it logs before the call to RandomPosition.

So, the instruction "_state.position = transform.localPosition;" in PlayerMotor.Awake happens before "character.transform.position = RandomPosition();" in TutorialPlayerObject.Spawn(), which is, I think, the reason why the character is always at Vector3.zero position (I think the PlayerMotor prevents the TutorialPlayerObject to set the entity position).


I have solved it by adding a method in PlayerMotor :

public void SetPosition(Vector3 position)
{
SetState(position, _state.velocity, _state.isGrounded, _state.jumpFrames);
}


and in TutorialPlayerObject.Spawn(), I replace the line "character.transform.position = RandomPosition();" by these lines:

PlayerMotor motor = character.GetComponent();
motor.SetPosition(RandomPosition());


So now it works for me, but is there a better way to solve this issue ? Maybe there is a missing step in the tutorial or something, I don't know.
FYI I am using Unity 2018.3.7f1.

Comments

  • NicolasVertical
    edited June 2019
    Options
    Actually, I figured out that my solution didn't really solve the issue, it just changed it to another issue. Now, when I launch a server and a client, the client doesn't see itself at the same position as the server.
    So I had to go back to get exactly the same code as in the advanced tutorial, and I have no solution for the original issue (which is : the character is at Vector.Zero position instead of RandomPosition)

    In TutorialPlayerController.cs, in ExecuteCommand method, I don't have the issue when I comment these lines:
    PlayerMotor.State motorState = _motor.Move(tutorialPlayerCommand.Input.Forward, tutorialPlayerCommand.Input.Backward,
                                                           tutorialPlayerCommand.Input.Left, tutorialPlayerCommand.Input.Right,
                                                           tutorialPlayerCommand.Input.Jump, tutorialPlayerCommand.Input.Yaw);
    
                //copy the motor state to the commands result (to send it back to the client, it allows correction from authoritative server)
                tutorialPlayerCommand.Result.Position = motorState.position;
                tutorialPlayerCommand.Result.Velocity = motorState.velocity;
                tutorialPlayerCommand.Result.IsGrounded = motorState.isGrounded;
                tutorialPlayerCommand.Result.JumpFrames = motorState.jumpFrames;
    When I comment these lines, the character is at the random position as expected. But if I let these lines, its position is somehow set to Vector.Zero (then, the character keeps falling because there is no ground below him).


    EDIT : Okay, I finally found the solution by comparing my code with the final sample. Actually, setting a random position in the Spawn method after BoltNetwork.Instantiate doesn't work either in the sample. But in the sample, the Player is not at Vector.Zero pos because the method "RandomPosition()" is called directly as a parameter of BoltNetwork.Instantiate (so that random position is assigned before the Awake() of PlayerMotor attached to the entity we instantiate). Calling Spawn() method to assign an other random position to the entity after BoltNetwork.Instantiate is useless and doesn't do anything (because the PlayerMotor doesn't take account of this modification and keeps the position value it got in Awake()).

    So the solution is simple:

    In TutorialPlayerObject.cs, in Spawn() method, replace this line:

    character = BoltNetwork.Instantiate(BoltPrefabs.TutorialPlayer);

    by this line:

    character = BoltNetwork.Instantiate(BoltPrefabs.TutorialPlayer, RandomPosition(), Quaternion.identity);
  • ramonmelo
    Options
    Hello @NicolasVertical

    Sorry about these details on the Tutorial, and we are happy to receive any feedback from our customers.
    We will consider your description to improve our documentation.

    If you want to get the latest version of the samples you can look for them on our public samples repo here, and if you want the samples for a specific Bolt version, look here.

    We create a tag on the repo for each released version of Bolt. And If you look at the master branch, you will always find the latest version and upcoming samples. Keep in mind that some parts of the code could be broken, as it's intended to work with our current development version.
  • Hi,

    Thanks for your answer and precisions.

    Sorry, I should have mentionned it, but the samples I used were from the unitypackage in the Asset Store.