Game Development Reference
In-Depth Information
The laser would look more realistic and intimidating with the addition of a particle system. In the
Project panel, open the Assets ➤ Sample Assets ➤ Effects ➤ Particle Systems ➤ Prefabs folder.
Find the Flare prefab, drag a copy to the Hierarchy, and make it a child object to LaserCube by
dropping it on LaserCube.
In MonoDevelop, edit the Laser script to add the three lines of code as follows:
#pragma strict
public var line : LineRenderer;
public var sparks : GameObject;
function Start () {
sparks = GameObject.Find("Flare");
line = gameObject.GetComponent.<LineRenderer>();
}
function Update () {
ShowLaser();
}
function ShowLaser () {
var ray : Ray = Ray(transform.position, -transform.up);
var hit : RaycastHit;
line.SetPosition(0, ray.origin);
if(Physics.Raycast(ray, hit, 50))
{
line.SetPosition(1, hit.point);
sparks.transform.position = hit.point;
} else {
line.SetPosition(1, ray.GetPoint(50));
}
}
Save the script and playtest. Now THAT is a laser!
The code breaks down as follows:
(1) public var sparks : GameObject;
Declare a reference variable sparks of type GameObject.
1.
(2) sparks = GameObject.Find(“Flare");
Find the reference to the Flare particle system prefab and assign it to sparks .
2.
(3) sparks.transform.position = hit.point;
3.
Assign the position of the sparks particle system at the point where the laser
raycast detects a collider.
 
Search WWH ::




Custom Search