Game Development Reference
In-Depth Information
Super cool, right? Notice that when playtesting, if the player character is struck by the laser, sparks
fly off him as if he is indestructible. While this might be awesome in another setting, here the laser
obstacle is meant to be more lethal.
In the Laser script, add the following to the variable declarations:
public var victim : GameObject;
This is declaring a reference variable victim of type GameObject.
To assign the reference to the player character, in the Start() function, add the following line of code:
victim = GameObject.Find("Third Person Character Ragdoll");
Finally, in the ShowLaser() function, insert the following lines of code:
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);
if (hit.collider == victim.collider) {
victim.GetComponent(GoRagdoll).GotoRagdoll();
}
sparks.transform.position = hit.point;
} else {
line.SetPosition(1, ray.GetPoint(50));
}
}
If the raycast detects a collider, it checks to see if the collider is that of the player character. If so, the
player character's GotoRagdoll() function is called to eliminate the player.
Save the script. In the Project panel open the Assets ➤ Prefabs folder. Drag the LaserCube from the
Hierarchy and drop it into the Prefabs folder for future use. Now go ahead and playtest.
For this obstacle a simple material and red color is sufficient, but a sample code snippet for setting
colors from the script would be to put the following lines of code in the Start() function:
line.material = new Material (Shader.Find("Particles/Additive"));
line.SetColors(Color.green, Color.blue);
Here you are making a new simple material, then setting the start and end colors of the line.
 
Search WWH ::




Custom Search