Game Development Reference
In-Depth Information
1. using UnityEngine;
2. using System.Collections; 3.
4. public class GunInput : MonoBehaviour { 5.
6.
//If true, the player don't have to release
7.
//the mouse button between shoots
8.
public bool continuous = true;
9.
10.
void Start () { 11.
12.
}
13.
14.
void Update () {
15.
//Send Shoot message on mouse click
16.
if(continuous){
17.
if(Input.GetMouseButton(0)){
18.
SendMessage("Shoot");
19.
}
20.
} else {
21.
if(Input.GetMouseButtonDown(0)){
22.
SendMessage("Shoot");
23.
}
24.
}
25.
}
26.}
Listing 51:
A script to read left mouse button and activate shooting
The variable
continuous
allows us to control the type of shooting, so we can for example
force the player to release mouse button before shooting again. An interesting detail in this
script is its independency from
RaycastShooter
, which makes it reusable with other types of
shooters or weapons. The only requirement that other shooters must have is the ability to
receive
Shoot
message sent by this script. The three scripts
RaycastShooter
,
RaycastShooter-
Animator
, and
GunInput
must be added to the gun game object. After adding them, we have
to set the muzzle for
RaycastShooter
, which is in this case the crosshair object. We can set