Simple use of unity's new input system (New InputSystem)

1. Download and install InputSystem in the unity registry of the package manager
Insert image description here
2. Add the component PlayerInput to the player, click CreateAction to create an InputAct
Insert image description here
InputAct, which is the player's input file, in which player input can be set
Insert image description here
3. Use,
for example, the player to control the movement of the character
In InputAct, the mobile input has been set by default.
Here is a two-dimensional vector value
W(0,1)A(-1,0)S(0,-1)D(1,0)
when pressing WD at the same time. , the forward distance (0.71, 0.71)
Insert image description here
determines whether the player is moving forward
. Keyboard y=1
Joystick: y>0, but the player will touch the joystick by mistake, so it is best to set an error value
and input the threshold variable (Threshold) to 0.1. If y>0.1, then move forward. Get player input
Insert image description here
in script

private Vector2 playerInputVec;
public void GetPlayerMoveInput(InputAction.CallbackContext context)
{
    
    
    playerInputVec = context.ReadValue<Vector2>();
}

Player movement example

void FixedUpdate()
{
    
    
    MovePlayer();
}
void MovePlayer()
{
    
    
    targetSpeed = isRuning ? runSpeed : walkSpeed;
    targetSpeed *= playerInputVec.magnitude;//当玩家没按下或没有推动摇杆,目标速度为0
    currentSpeed = Mathf.Lerp(currentSpeed, targetSpeed, 0.5f);
    //*TiredIndex,目的是主角运动速度与体力消耗相关联,当耗尽则不会在运动,只有体力恢复才能继续运动
    animator.SetFloat("Sports", currentSpeed*TiredIndex);
}

Go back to unity, click on the protagonist, open the PlayerInput component
click event on the right side - Player, click the plus sign on Move, and drag the script mounted on the player to the black box (be sure to drag the script in, drag the game directly object), select the input method GetPlayerMoveInput just written on the right.
Insert image description here
4. If the default input file is not enough, you can add it yourself.
For example, to add a running input , click
InputAct
and click the plus sign to create a new Action and name it according to your own behavior.
Insert image description here
Select the type on the right side
Insert image description here
, then click the plus sign on the right side of Action to add Binding
Insert image description here
. Click Binding. You can change the name on the right side, select Path (input type, such as KeyBoard keyboard, etc.) and
set the specific button in the Use in control scheme below. Can
Insert image description here

Guess you like

Origin blog.csdn.net/weixin_51565051/article/details/132227444