Graphics Reference
In-Depth Information
public GameObject ObjectToSpawnOnTrigger;
public Vector3 offsetPosition;
public bool onlySpawnOnce;
public int layerToCauseTriggerHit= 13; // this should be set to the
// number of the camera layer
private Transform myTransform;
void Start ()
{
Vector3 tempPos=transform.position;
tempPos.y=Camera.main.transform.position.y;
transform.position=tempPos;
// cache transform
myTransform=transform;
}
void OnTriggerEnter(Collider other)
{
// make sure that the layer of the object entering our
// trigger is the one to cause the boss to spawn
if(other.gameObject.layer!=layerToCauseTriggerHit)
return;
// instantiate the object to spawn on trigger enter
Instantiate(ObjectToSpawnOnTrigger,myTransform.position+
offsetPosition,Quaternion.identity);
// if we are to only spawn once, destroy this gameobject
// after spawn occurs
if(onlySpawnOnce)
Destroy (gameObject);
}
}
4.3.3 Path Spawner
This script fulfills quite a specific need, albeit one that may crop up frequently in game
development. The path spawner makes a new object appear in the game and tells its
waypoint-following code which path to follow (which waypoint controller script to use for
finding waypoints).
In the example game Interstellar Paranoids from Chapter 14, it is used for the path-
following enemies in each level. Waypoint paths are stored in prefabs, which may be dragged
into each scene to build levels with. The Path_Spawner.cs script component is attached to
the parent gameObject in the path prefabs, identifying which enemies to spawn, and then
as they are spawned, telling them to use the waypoint path from the prefab.
Search WWH ::




Custom Search