Game Development Reference
In-Depth Information
If you consider GeneralWeapon as the processing core of shooting mechanism, then you
would recognize that it needs both input and output handlers. The input handler is respons-
ible for aiming the weapon and calling Shoot() function based on player input. On the other
side we need an output handle which receives OnWeaponFire message and translates it to
actual effect on the scene. Let's begin with the input handler, since it is common among the
three weapons, unlike output handlers. Remember that we are using the mouse to aim at
targets and shoot them. Therefore, we need a script that looks at the position of the mouse
pointer. This script is MousePointerFollower shown in Listing 83. This script must be ad-
ded to player game object, which is the parent of the three weapons.
1. using UnityEngine;
2. using System.Collections; 3.
4. public class MousePointerFollower : MonoBehaviour { 5.
6.
//A marker that can be placed on the current target
7.
public Transform targetMarker; 8.
9.
void Start () {
10.
//Hide the marker behind the player
11.
targetMarker.position =
12.
transform.position - Vector3.forward;
13.
}
14.
15.
void Update () {
16.
//Try to find the point where mouse points
17.
Ray camToMouse =
18.
Camera.main.ScreenPointToRay (Input.mousePosition); 19.
20.
RaycastHit hit;
21.
if(Physics.Raycast(camToMouse, out hit, 500)){
22.
//An object has been found, look at it
23.
transform.LookAt(hit.point);
24.
//Move the marker to the hit point
25.
targetMarker.position = hit.point;
26.
//Move the marker a little bit towards us
27.
targetMarker.LookAt(transform.position);
28.
targetMarker.Translate(0, 0, 0.1f);
29.
} else {
Search WWH ::




Custom Search