Game Development Reference
In-Depth Information
Step 3. The objective in this hands-on session will be to get the
rocket ship to attack the planet. To begin, create a new JavaScript
file and call it attack . Attach the file to the Enemy game object in
the Hierarchy. Open attack.js with the script editor. Add the script
from Listing 2.2 .
Listing 2.2 Script to Make One Game Object Face Another
private var target: GameObject;
function Start ()
{
target = GameObject.Find("Base");
}
function Update ()
{
this.transform.LookAt(target.transform.position);
}
Step 4. Save and play. The rocket ship will turn to face the planet.
In Update() , the function LookAt() is used. It takes a target position
and turns the game object to face it. In Start() , the target is set to
the object called base, which is also the name given to the object.
This object is called “Base.” To see this in the Inspector, select the
base in the hierarchy. The name of the planet object is shown in
Figure 2.20 .
Fig 2.20 The Inspector view
of a game object's name.
Search Nedrilad ::




Custom Search