Game Development Reference
In-Depth Information
This for loop will create and position one new prefab with each iteration.
Since the lasers are quite narrow, placing them 0.5 units apart means you
need twice as many, and so the for loop iterates two times the length of
the zone.
4.
(5) var position: Vector3 = Vector3(-3.8, 12, zPos);
5.
Declare a Vector3 type variable named position to assign the initial position
of the laser prefab. In this zone, the starting x and y coordinates for the laser
prefabs are fixed, so only the z coordinate of each prefab needs to be varied
and assigned in order to space them out along the zone.
(6) Instantiate(prefab, position, Quaternion.identity);
6.
Instantiate the laser prefab with the new position and no rotation.
(7) zPos = zPos + 0.5;
7.
Advance to the next position by adding 0.5 units to the newly instantiated
prefab's z coordinate.
Save the script and attach it to the Hazard Setup game object in the Hierarchy. Select Hazard Setup
in the Hierarchy, then select the Laser prefab in the Prefabs folder in the Project panel and assign it
to the Prefab variable in the Laser Layout script component in the Inspector. Playtest, and take note
that only one laser emits sparks when it hits the elevated track where it is supposed to (Figure 10-9 ).
Figure 10-9. All but one of the sparks particle systems appears out of place
 
Search WWH ::




Custom Search