Game Development Reference
In-Depth Information
One important issue here is: how to compute the new x position of the camera? The best
way to answer the question is by an example: suppose that the maximum distance to the
right is 1.5 as defined in line 8, and the character moves until it reaches the x position of
1.6, while the x position of the camera is still 0. Now if we compute the difference between
the two positions, it is going to be 1.6 - 0 = 1.6, which is greater than the allowed value of
1.5. Therefore, we need to move the camera to the right. We need also to keep the charac-
ter 1.5 units to the right of the camera, to allow the camera to move along and follow the
character. In order to get the correct new position for the camera, we subtract the maximum
allowed value from the current position of the character, which equals to 1.6 - 1.5 = 0.1. So
0.1 is the new x position we need to move the camera to. Camera movement is performed
in line 30.
The same method applies to the case in which the character is to the left of the camera.
The difference is that we add the value of maxDistanceLeft to the current position of
the character to get the new position for the camera, like in line 34. The same applies
also to the y movement of the character, along with the values of maxDistanceUp and
maxDistanceDown .
The final step is to assign the new position we have computed in camPos variable to the
position value of the camera transform, which we do in line 46. You can see the final result
in scene3 in the accompanying project.
Search WWH ::




Custom Search