Categories: Unity, C #, VS2015
Date Created: 2016-04-21 I. INTRODUCTION
Unity can handle the input of standard game peripherals such as sticks, gamepad, and steering wheel, using the following method:
The virtual keys need to be configured in the input manager to map the input messages of the peripherals to the virtual keys or input axes, which can be used in the script.
Unity defaults to a number of virtual keys that have been mapped to the joystick buttons, including Fire1, Fire2, Fire3, jump, and virtual axes horizontal and vertical, which can be used directly in the script. Ii. Examples of basic usage
Example (demo4_1_producerobot.unity)
This example shows how to create a robot in a demo4_1_producerobot.unity scene.
The code for the ProduceRobot.cs file is as follows:
usingUnityengine;usingSystem.Collections; Public classproducerobot:monobehaviour{ PublicGameobject robot;//Robot Object Public floatProducerate =0.5f; Private floatNextproduce =0.0f; voidUpdate () {//Fire1 the No. 0 Number button (or left mouse button) for the joystick if(Input.getbutton ("Fire1") && Time.time >nextproduce) {Nextproduce= Time.time +producerate; //when the Fire1 key (or the left mouse button) is pressed, a robot is cloned every 0.5 seconds to appear in the scene varPosition =NewVector3 (Random.range (-5,5),1, Random.range (-5,5)); Instantiate (robot, position, robot.transform.rotation); } }}
To run the preview effect:
"Unity" 7.4 Game Peripheral input