Game Development Reference
In-Depth Information
It carries on just where it left off. This means location is retained during deactivation, so you will have
to specify its starting location each time he starts the fly-by.
22.
Add the following variables:
Vector3 startLocation; // starting spot
Vector3 endLocation; // out of view
There is no rotation involved, so you need only save the three position values in a Vector3 struct.
When the object is reactivated, you will have to set it back to the start position. By setting up a
function to handle the task, you can call it from both the
Start
function and any other object.
1.
Holding the shift key down to constrain to a horizontal transform, drag the
Stork Group to the left of the screen until it is no longer showing in the
game view.
2.
Make note of the X position, and calculate a safe total distance of change.
A distance value of -25 should be more than enough.
3.
Add the offset variable:
float offSetX = -25f; // distance to the other side
The stork will have to be deactivated at the start, so you will set up the values in the
Awake
function
instead of the
Start
function.
Awake
is evaluated before the
Start
function and is used to locate and
identify objects that will be deactivated in the
Start
function.
Above the
Start
function, create an
Awake
function:
4.
void Awake () {
startLocation = transform.position; // store the starting location
endLocation = new Vector3(startLocation.x + offSetX , startLocation.y,
startLocation.z);
}
You can initiate the action from the
Start
function so the player will get a preview of what happens
when the garden is overrun with zombie bunnies.
In the
Start
function, add:
5.
Initialize(); // set location
Next, create the
Initialize
function:
6.
void Initialize () {
// reset the start position
Vector3 tempLocation = transform.position; // make a variable to hold the value
tempLocation = startLocation; // change the value
transform.position = tempLocation; // assign the new value
}