Game Development Reference
In-Depth Information
First, designate the spawn point. Create an empty game object using the top menu Game
Object ➤ Create Empty and position it on top of the cube. Rename it SpawnPoint01.
Second, create a trigger point for the character to respawn on top of the cube. Again use the top menu
Game Object ➤ Create Empty, then in the Inspector select Add Component ➤ Physics ➤ Sphere
Collider. Check Is Trigger and change Radius to 1. Name it RespawnPoint01Trigger01. That's a pretty
long name, but if you choose to add more respawn points and more triggers, each will have a unique
descriptive name. Now position the game object on the yellow platform.
In the Project panel, select Create ➤ Javascript, name the new script Respawn01, and open it in
MonoDevelop for editing. Delete the Start() and Update() functions, then add the following code:
#pragma strict
public var respawnPosition: Transform;
function OnTriggerEnter(other : Collider) {
other.gameObject.transform.position = respawnPosition.position;
}
This breaks down as follows:
(1) public var respawnPosition: Transform;
Declares a public Transform type reference variable named respawnPosition.
(2) other.gameObject.transform.position = respawnPosition.position;
In this case the other object impacting the trigger collider is the player character. Here you assign
the position reference held by the respawnPosition variable to the player character game object's
transform position.
Save the script and attach it to the RespawnPoint01Trigger01 game object. With the
RespawnPoint01Trigger01 game object selected in the Hierarchy, drag the SpawnPoint01 game
object from the Hierarchy and drop it into to the Respawn Position field of the Respawn01 script
component. This assigns the SpawnPoint01 transform.position (x, y, z) coordinate values to the
respawnPosition variable.
Save the scene and play. Run the character around the platform until it trips the trigger and
disappears from the platform, only to instantly respawn at the designated point on top of the highest
blue cube. Success!
Your player audience would have no idea that there might be a magical solution to get to the top of
the cubes, so you want to give some kind of cue. In the Project panel, drill down to the Prototyping
➤ Prefabs folder and open it up. Drag a Question Coin to the Scene and position it over the
RespawnPoint01Trigger01 trigger collider. Do not click Apply in the Inspector as you do not want
these changes applied to all Question Coin prefabs. In the Hierarchy rename it Question Coin 01.
Search WWH ::




Custom Search