Unity’s new input system InputSystem

1. The old input manager

In Unity, the old input manager (InputSystemOld) is used by default

Insert image description here

In the script, the Input class can be used to obtain the user's input operations and access the multi-touch or acceleration sensing data of the mobile device. The Input class can read the keys set in the input manager and monitor the user's input in the Updata function.

1.Virtual axis

You can see the relevant settings of the virtual axis in the project settings.

Insert image description here

For example, in the input manager, you can see the corresponding keys

Taking Horzizontal as an example, what is monitored is the AD and left and right arrows of the keyboard.

float value=Input.GetAxis ("Horzizontal");Virtual axis value using smoothing filter. Not pressing is 0, positive direction is 1, and negative direction is -1. Pressing it will gradually transition from 0 to 1 or -1. The change process is determined by the sensitivity.

float value=Input.GetAxisRaw ("Horzizontal");//Virtual axis value without smoothing filter. (Values ​​1, 0, -1)

For example, use AD to control the left and right movement of a character.

private CharacterController characterController;//使用CharacterController组件控制物体
private void Start()
{
    
    
    characterController = GetComponent<CharacterController>();//得到自身的CharacterController组件
}
private void Update() 
{
    
    
    float tmp_CurrentSpeed = 1.7f;//速度
    float tmp_Horizontal = Input.GetAxis("Horizontal");//按下AD返回一个1~-1随时间过渡的float值
    Vector3 movementDirection = characterTransform.TransformDirection(new Vector3(tmp_Horizontal,0,0));//移动的方向,参数为x,z,y
    characterController.Move(tmp_CurrentSpeed * Time.deltaTime * movementDirection);//控制物体移动
}

2. Get keyboard input

Jump monitors keyboard spaces
Insert image description here

bool result=Input. GetButton(“Jump”); //Monitor whether the corresponding button is entered

bool result=Input. GetButtonDown(“Jump”); //Monitor whether the corresponding button is fully pressed

bool result=Input. GetButtonUp(“Jump”); Monitor whether the corresponding button is released

For example, controlling the jumping of a character

private CharacterController characterController;//使用CharacterController组件控制物体
private void Start()
{
    
    
    characterController = GetComponent<CharacterController>();//得到自身的CharacterController组件
}
private void Update() 
{
    
    
    float tmp_CurrentSpeed = 1.7f;//速度
    float tmp_JumpHeight = 3.0f;//跳跃高度
    float tmp_Horizontal = Input.GetAxis("Horizontal");//按下AD返回一个1~-1随时间过渡的float值
    Vector3 movementDirection = characterTransform.TransformDirection(new Vector3(tmp_Horizontal,0,0));//移动的方向,参数为x,z,y
    if(Input.GetButtonDown("Jump"))//当按下空格后
    {
    
    
        movementDirection.y = tmp_JumpHeight;
    }
    characterController.Move(tmp_CurrentSpeed * Time.deltaTime * movementDirection);//控制物体移动
}

3. Get mouse input

bool result=Input.GetMouseButton(0);//When the mouse holds down the left button

bool result=Input.GetMouseButtonDown(1);//When the mouse right button is pressed

bool result=Input.GetMouseButtonUp(2);//When the mouse lifts the middle button

2. New input system package

1. Before use

NOTE : The new input system requires Unity 2019.4+ and .NET 4 runtime. It does not work on projects using the old .NET 3.5 runtime.

To install a new input system, open Unity's Package Manager (Menu: Window > Package Manager ). Select the input system package from the list and click Install .

By default, Unity's classic input manager ( UnityEngine.Input) is active and support for new input systems is inactive.

This allows existing Unity projects to keep working as is. When installing the input system package, Unity will ask you if you want to enable the new backend. If you click Yes , Unity will enable the new backend and disable the old backend, and the editor will restart.

2. When using

Add control PlayerInput to control object

Insert image description here

Create an input action, right-click->Create->InputAction, link it to the Action of the PlayerInput component, and set the buttons according to the needs of the project
Insert image description here

Set Action Responses Once a component has its Actions, you must set a response for each Action. PlayerInput allows you to set the response in several ways, using the Behavior property in the Inspector window.
Insert image description here

The screenshot above uses Invoke Unity Events, which uses UnityEvents in the same way as Unity UI. Unity displays an event for each Action linked to a component. This allows you to directly wire the target method for each event. The picture above shows the Jump, LongJump, and TouchPosition behaviors in Action, which are respectively connected to the public modified Jump, LongJump, and Gettouchposition methods in the PlayerController script.

Note that in the method script, you need to use the namespace using UnityEngine.InputSystem; and obtain the PlayerInput control.


I referred to the following content during the learning process. I sincerely thank those who shared the knowledge!

2022 Unity Technology |Unity Official Manual

Guess you like

Origin blog.csdn.net/qq_18931093/article/details/130715761