Now that you have nav mesh generation working, the logic for spawning the players in the PlayerServer script needs to be updated.
Create Variables
In the PlayerServer script, add 2 new variables to the top of the script that will hold the spawn point position and rotation when they have been received. This will then be used for players who join late.
local spawnPointPosition = nillocal spawnPointRotation = nilCode language:Lua(lua)
Create OnPlayerJoined Function
Create a new function that will be called when the player joins the game. This function will look and see if the spawnPointPostion is nil. If it is not nil then the player will be spawned at the position set in the spawnPointPosition variable. If the variable is nil, then the player will be despawned, meaning that the map and nav mesh generation is still in progress.
The SpawnPlayers function will need to be updated so that it sets the spawnPointPosition and spawnPointRotation variables. This will be called when the nav mesh generation has been completed.
localfunctionSpawnPlayers(position, rotation) spawnPointPosition = position + (Vector3.UP * 150)
spawnPointRotation = rotation
for _, player inipairs(Game.GetPlayers()) do player:Spawn({
position = spawnPointPosition,
rotation = rotation
})
endendCode language:Lua(lua)
Connect Player Joined Event
Connect the OnPlayerJoined function to the playerJoinedEvent so it checks each player that joins the game if they can be spawned in or not.
local spawnPointPosition = nillocal spawnPointRotation = nillocalfunctionActionPressed(player, action)if(action == "Toggle Fly Mode") thenif(player.isFlying) then player:ActivateWalking()
else player:ActivateFlying()
endendendlocalfunctionOnPlayerJoined(player)if spawnPointPosition ~= nilthen player:SetWorldPosition(spawnPointPosition)
player:SetWorldRotation(spawnPointRotation)
else player:Despawn()
endendlocalfunctionSpawnPlayers(position, rotation) spawnPointPosition = position + (Vector3.UP * 150)
spawnPointRotation = rotation
for _, player inipairs(Game.GetPlayers()) do player:Spawn({
position = spawnPointPosition,
rotation = rotation
})
endendInput.actionPressedEvent:Connect(ActionPressed)
Events.Connect("SpawnPlayers", SpawnPlayers)
Game.playerJoinedEvent:Connect(OnPlayerJoined)
Code language:Lua(lua)
Test the Game
Test the game and make sure the following work:
The player is not spawned while the map and nav mesh is being generated.
The Player is spawned after the nav mesh has been generated. You can look at the Event Log for the progress.
The enemies no longer walk through walls.
Nav Mesh Settings
The nav mesh generator and manager has settings you can change if you want to lower the size of the tile to get a tighter nav mesh, and also to see the grid at runtime for debugging. Change the custom property TileSize on the DDNavMeshGenerator group in the Hierarchy for smaller tiles, and enable AutoStartNavMeshVisualization to see the grid at runtime.
This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.
Strictly Necessary Cookies
Strictly Necessary Cookie should be enabled at all times so that we can save your preferences for cookie settings.
If you disable this cookie, we will not be able to save your preferences. This means that every time you visit this website you will need to enable or disable cookies again.
3rd Party Cookies
This website uses Google Analytics to collect anonymous information such as the number of visitors to the site, and the most popular pages.
Keeping this cookie enabled helps us to improve our website.
Please enable Strictly Necessary Cookies first so that we can save your preferences!