Spawn Point
INFO
It takes about 5 minutes to read this article
Use the [spawn point] in the project to determine the player character's spawn position and direction. Generally, spawn points are used in games to control the player's refresh location, such as appearing at the starting point of the first level, or distributed throughout the map.
Spawn Point
The spawn point is an object that determines the location and orientation of the player character when it is first generated or regenerated in the project. Each project comes with a [Spawn Point] when it is created: PlayerStart gameobject. In addition, you can also find the [Spawn Point] object in the [Gameplay Object] column and drag it into the scene. When there are multiple [spawn point] objects in the scene, the player character will randomly select one as the location for generation or refresh.
Create a spawn point
Created by placing an asset:
The [spawn poin] itself can exist in the game scene as a game object. You can find the [Gameplay Object] column in the [Asset Library]. In the Gameplay Object category, find the [Spawn Point] and drag it into the [Scene] or [Object Manager] to automatically generate a new Spawn Point. Selecting the spawn point allows you to modify its position and rotation (z axis only).
- Find the [Spawn Point] in the [Asset Library]
- Drag the object into the scene or [Object Manager]
- In the [Object] column of the [Object Manager] on the right, find the corresponding [Spawn Point] and customize its position and rotation (z-axis only).
TIP
[Spawn Point] is a game object that only exists on the server.
Custom Spawn Point
The properties of the Spawn Point will determine the position and orientation of the player character when it is refreshed:
Position of the player character: Position
The orientation of the player character: Rotation (z-axis)
In the [Object Manager], find the corresponding [Spawn Point] in the [Object] column. After selecting it, we can view its property panel. Through the property panel, we can modify the coordinates and rotation (z-axis only) of the [Spawn Point]. Generally speaking, the [Spawn Point] does not need to be operated in the script. When the game is running, the player character will randomly select a spawn point in the scene as the refresh position.
Spawn Point location
The position where the player is generated is determined by the position of the [Spawn Point]. You can directly select the [Spawn Point] in the [Object] column of the [Object Manager], find [Transform] in the [Properties Panel], and change the [Position] property of the spawn point to change the position where the player is generated.
Spawn Point rotation
The direction in which the player is generated is directly determined by the direction of the spawn point. You can change the position where the player is generated by selecting the Spawn Point in the Object column of the Object Manager, finding Transform in the Properties Panel, and changing the Rotation property of the spawn point. At the same time, the orientation of the [Spawn Point] will be indicated by a small blue arrow in the scene:
TIP
The [Spawn Point] actually does not have its own private properties. Its position and rotation properties are inherited from the parent class GameObject
. The X and Y values in the [Rotation] property of the [Spawn Point] cannot be modified, because modifying the X and Y rotations of the character will cause unpredictable problems. The [Scale] property of the [Spawn Point] cannot be modified either.
Using the Spawn Point
Get the Spawn Point
In the Object Manager, under the Object column, click the PlayerStart object.
Use the asyncFindGameObjectById
interface to obtain the gameObjectId of the [Spawn Point]:
- Select the [PlayerStart] object and right-click [Copy GameObject ID] to get its gameObjectId. Note the difference between GameObject ID and asset ID.
- Add the following code to the
onStart
method of the script: The code will asynchronously search for the object corresponding to the ID and receive it as the [Spawn Point].
if(SystemUtil.isServer()) {
let spawnPoint = await GameObject.asyncFindGameObjectById("06D5DBFC");
console.log("spawnPoint's gameObjectId is " + spawnPoint.gameObjectId);
}
if(SystemUtil.isServer()) {
let spawnPoint = await GameObject.asyncFindGameObjectById("06D5DBFC");
console.log("spawnPoint's gameObjectId is " + spawnPoint.gameObjectId);
}
Use script mounting to obtain:
- Add the following code in the onStart method of the script: The code gets the object mounted by the script.
if(SystemUtil.isServer()) {
let spawnPoint = this.gameObject as GameObject;
}
if(SystemUtil.isServer()) {
let spawnPoint = this.gameObject as GameObject;
}