Game Development Reference
In-Depth Information
private var attackDistance = 60;
private var retreatDistance = 10;
private var speed = 0.3;
function Start ()
{
target = GameObject.Find("Base");
}
function Move()
{
transform.rotation = Quaternion.Slerp(transform.
rotation,
Quaternion.LookRotation(direction),
rotationSpeed * Time.deltaTime);
this.transform.Translate(Vector3.forward * speed);
}
function Update ()
{
direction = target.transform.position -transform.position;
//determine state
if(direction.magnitude > attackDistance)
{
state = "ATTACK";
}
else if (direction.magnitude < retreatDistance)
{
state = "RETREAT";
}
//act on state
if(state == "ATTACK")
{
Move();
}
else if (state == "RETREAT")
{
direction *= -1;
Move();
}
}
Step 10. Save and play. The rocket will go toward the planet and
when it gets too close will turn away. Once it reaches the bounds of
the attack distance it will turn around and approach the planet again.
Major changes to this code are the variables added to the top and
the Move() function. The reason for creating variables for all the static
values in the code is to make them easy for editing. If you want to
play around with the values to see how it changes the behavior of
Search Nedrilad ::




Custom Search