Game Development Reference
In-Depth Information
4.
Select the child, Main Camera, remove its Smooth Follow component, and
set its tag to Untagged.
5.
Look at Main Camera's Transforms.
When Main Camera was parented, its transforms were all set to 0 because transforms in the
Inspector are always “local.” When an object has a parent, its transforms are relative to its parent.
Because they were just duplicated, the offsets between the parent (with the SmoothFollow
component) and child (with the Camera component) are all 0.
6.
In the Garden Gates' Distance Detector component, assign the Main Camera
as its The Camera parameter.
7.
Click Play, and test to make sure the camera works as it did before the
changeover.
The results should be the same, but now the object with the camera component can be moved
independently of the object that is used to calculate the distance. You will be setting its local z, or
forward direction, to an offset derived from the distance between the gnome and the Gateway center.
Logically, you only want to adjust the camera's position when it is close to the gateway, without letting
it get too close to the character. A range of 3.0 to 1.0 is a good starting point. When the gnome gets
within 3.0 meters of the center of the gateway, the camera will start getting closer to the character.
When the gnome is 1 meter from the gateway center, the camera will not be able to get any closer to
the gnome. Let's add a condition for the range and watch the printouts to see if it looks okay.
Change the print statement so it is wrapped in conditional as follows:
1.
if (dist < 3.0f && dist > 0.5f) {
print (dist);
}
else print (""); // clear the status line
2.
Save the script, and test to make sure the values reported are always within
the stipulated range.
You could get fancy with the distance calculations, but in this script you will just use the total range
minus the current range. For example, when the gnome is 2 meters from the gateway's center, the
camera will be 3.0 - 2.0, or 1.5 closer to the gnome on its local Z, or forward, direction. At 1 meter
away, the camera will be 2.5 meters closer to the gnome than its base distance.
Replace the print statement with the following:
3.
Vector3 pos = theCamera.transform.localPosition; // make a variable to hold the current
local position of the camera
pos.z = 3.0f - dist; // assign the inverse of the distance to the z part of the temporary
variable
theCamera.transform.localPosition = pos; // assign the new position
Comment out the else line.
4.
5.
Save the script, and test the new code.
 
Search WWH ::




Custom Search