Game Development Reference
In-Depth Information
Open the Tag drop-down menu and you'll see a list of commonly used tags. While some built-in tags
appear, you want to create one called Enemy, so select Add Tag... at the bottom of the menu to
open the Tag Manager in the Inspector (Figure 7-20 ).
Figure 7-20. Tag and Layer Manager in the Inspector
If none of the Elements are empty, you can increase the value of Size. Bear in mind that a game
object can only have one tag, but you can create as many different tags for your game as you like.
Add Enemy to an empty element in the Tag list, then reselect Sphere_001 in the Hierarchy. Now
when you select the Tag drop-down menu in the Inspector, Enemy appears as a choice, so go ahead
and select it. Tag the other four Sphere game objects as Enemy as well.
In MonoDevelop, edit your Update() function to the following:
function Update () { var hit : RaycastHit;
if(Physics.Raycast(transform.position+ Vector3(0, 0.5, 0), transform.forward, hit)) {
print ("Collided with game object " + hit.collider.gameObject.name);
if (hit.collider.gameObject.tag == ("Enemy"))
{
Destroy(hit.collider.gameObject);
}
}
}
Save and playtest, and now any evil sphere the ray intersects with is destroyed. Run the player
character around in all directions, and notice that the platform and block obstacles are not
destroyed, only the spheres.
Tags aren't specific to raycasting; they are for easier management of groups of game objects in your
scripts.
Search WWH ::




Custom Search