[SOLVED] How to make a random point spawn for a player before the game starts?

Hi! I’m working on a game in which players jump on platforms. The game has several points from which players can start the game. How to make so that before the start of the game there was a random selection of the starting point of the game? I tried to use the teleport() method, but when you start the game, the player still starts from the same point and then teleports to a random point.

How many points are there? See if this helps.

Let’s say there are 5 points from which the player can start. Then, you can set 5 different player entities on each of the points. You can then do

pc.math.random(1, 5);

Then use if statements to check which number the computer determined. Depending on the number selected by the computer, activate the relevant player entity and disable the others.

Thanks for the answer, but I already built the algorithm in this way, a code fragment:

var value = pc.math.random(11, 29).toString().slice(0, 2);
        
if (value == '11') {
    entity.rigidbody.teleport(this.spawn1);
}
        
if (value == '12') {
    entity.rigidbody.teleport(this.spawn2);
}
        
if (value == '13') {
    entity.rigidbody.teleport(this.spawn3);
}
        
if (value == '14') {
    entity.rigidbody.teleport(this.spawn4);
}

This code works correctly, but the problem is that it runs after the application starts. I want the script to run and teleport the players to a random position before launching the application. I tried to set the priority of loading scripts, but it did not help.

Are you running the code in the update function? Try in the initialize function.

I call this code in the initialize function.

The link to the video.

Can you try making different numbers of players and enabling / disabling them?

This is a multiplayer game. I have the essence of the player and the essence of another player that is turned off. When you connect a new player, it copies the essence of another player and includes it on the scene.

By analogy with this project.

Can you try the same thing by forking the multiplayer tutorial? Or are you doing exactly the same thing?

Yes, I do the same, only my network code does more. But I think the network has nothing to do with it, because this problem is even in single player. Probably this code needs to be performed in the preload, but I have not found any info on whether there are any such mechanisms in the playcanvas.

Only such an idea comes to mind - to collect all the players in the room before the match starts and then teleport them to random positions.

I think that would work

Thank you for participating in the discussion and help in solving the problem. :slightly_smiling_face:

You’re welcome. Could you please help me out a bit once you are done with your game?

It’s just that I needed me help creating a multiplayer racing game which I would like to publish in the play store and elsewhere.

I would be very grateful if you could help me out with the game.

Or else, we could develop the game you were working on as part of a similar project. I just needed a bit of help with developing games - We need help from a PlayCanvas Coder! - As I said before, help on any of these games would be appreciated greatly.

I have been developing games for only 3 months. I have very little experience and I cannot estimate the amount of time spent on my game, since the project is quite large. After completing this project, I may possible be able to help you with your project.

I haven’t been developing games for too long myself, having just released my first one - https://www.crazygames.com/game/space-escape.

Thanks a lot for offering to help. I am eagerly waiting for us to collaborate. Just email me at renegadels@yandex.com once you have finished your project. Alternatively I could help you with this project too.