Game Development Reference
In-Depth Information
The script has two public variables: launchPower , which is the magnitude of the launch
force we are going to multiply with distance specified by the player, and lifeTime which is
the amount of time to keep the projectile after launching it. In addition to public variables,
we have two flags: mousePressed which becomes true when the player presses the left
mouse button over the projectile, and launched , which stays false until the projectile has
been launched. The second flag is necessary to ensure that the player is allowed to launch
the projectile only once. Additionally, we have launchPosition , which is the other end of
the line that specifies the direction of the force to be applied. Finally, we need refer-
ences to both the main camera and the line renderer that is attached to the object. The
variable Camera.main gives us the reference to the main camera.
Launching the projectile is handled through in OnMouseDown() and OnMouseUp()
functions. After holding the mouse during aiming, LateUpdate() handles updating the line
that indicates launching direction and magnitude. OnMouseDown() function is called when
the player clicks the mouse over the object, so we use this function to set mousePressed
flag to true . After pressing the mouse over the projectile, the player should move the mouse
to set the launch direction. Therefore, we use LateUpdate() function to update the indication
line, which applies only if the projectile has not yet been launched and the mouse is still
pressed. The first step is convert the position of the mouse pointer from screen coordinates
to 3D space coordinates, in order to get the position of the other end of the line. To help us
in this task, the camera provides us with the function ScreenPointToRay() .
Search WWH ::




Custom Search