Graphics Reference
In-Depth Information
a visual rotation to show that the ship is turning left or right—it makes the spaceship tilt
nicely when the left or right keys are held down:
Vector3 tempRotation= myTransform.eulerAngles;
tempRotation.z= horizontal_input * -30f;
myTransform.eulerAngles=tempRotation;
The movement of the spaceship is applied to its transform.localPosition. The reason
for this is that, in Interstellar Paranoids , the player is parented to the camera as it makes its
way through the level. We do not want to affect the global position of the ship; it needs to
stay within the coordinate space of the camera to stay on screen, and only its local position
is changed when we move it around:
// move our transform to its updated position
myTransform.localPosition += new Vector3(moveXAmount, 0,
moveZAmount);
Limit checking is very basic for this script, with arbitrary values for the left and right
and an amount added to the original local z position of the ship. The center of the screen
is at 0 (we start the camera at zero on the x -axis) to keep things clear, using the variable
limitX and a negative −limitX to stop the ship leaving the screen.
// check the position to make sure that it is within boundaries
if (myTransform.localPosition.x <= -limitX ||
myTransform.localPosition.x >= limitX)
{
Mathf.Clamp is a useful built-in utility function to take a value and clamp it between
a minimum and a maximum value. When you pass a value into Mathf.Clamp, if it is more
than the maximum value, then the return value will be the maximum value. If the value
passed in is less than the minimum value, it will return the minimum.
In this function, Mathf.Clamp is used to populate the float variable thePos with a value (to
use for the z position value) within both horizontal and, later in the code, vertical position lim-
its (limitX and −limitX or limitZ and originZ) from the localPosition of the player transform:
thePos = Mathf.Clamp( myTransform.localPosition.x, -limitX, limitX);
Once the variable thePos contains a clamped (limited) value for positioning, the
transform position is set:
myTransform.localPosition = new Vector3(thePos,
myTransform.localPosition.y, myTransform.localPosition.z);
}
The final part of this function deals with the clamping of the z position in the same
way that the x was clamped:
// we also check the Z position to make sure that it is within
// boundaries
if (myTransform.localPosition.z <= originZ ||
myTransform.localPosition.z >= limitZ)
{
thePos = Mathf.Clamp( myTransform.localPosition.z, originZ,
limitZ);
Search WWH ::




Custom Search