?文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-592265.html
目錄
安裝InputSystem
?在編輯的腳本中使用 InputSystem生成的腳本
Unity版本:2019.2.3f1
安裝InputSystem
菜單欄/Window/Package Manager/Input System
?工程面板內(nèi)?右鍵-->創(chuàng)建Input Actions?
?選中New Controls改名為PlayerControls?然后屬性?面板按下Edit asset
?Action Maps添加:PlayerMovement
?文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-592265.html
Actions添加:New action?改名為MovementAction?
Properties項(xiàng)? ? 修改ActionType=Pass Through
? ? ? ? ? ? ? ? ? ? ? ? 修改ControlType= Vector2
?在MovementAction項(xiàng)點(diǎn)擊+號(hào)?選擇Add 2D Vector Composite
?
?生成WASD
?綁定Up、Down、Left、Right,如此類(lèi)推
?回到PlayerControls屬性面板?勾選Generate C# Class[*]
工程面板就生成了一份?PlayerControls.cs?腳本?
?在編輯的腳本中使用 InputSystem生成的腳本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerLocomotion : MonoBehaviour
{
PlayerControls inputActions;//聲明 InputSystem的腳本對(duì)象
public new Rigidbody rigidbody;
Vector2 movementInput;//存儲(chǔ) WASD輸入的值
[Header("Stats")]
[SerializeField]
float movementSpeed = 5;
// Start is called before the first frame update
void Start()
{
rigidbody = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
Vector3 vector3 = new Vector3(movementInput.x * movementSpeed, 0, movementInput.y * movementSpeed);
rigidbody.velocity = vector3;//給剛體 這個(gè)方向的速度
}
public void OnEnable()
{
//獲取設(shè)備上的輸入
if (inputActions==null)
{
inputActions = new PlayerControls();
//綁定輸入的值
inputActions.PlayerMovement.MovementAction.performed += outputActions => movementInput = outputActions.ReadValue<Vector2>();
}
inputActions.Enable();//啟用
}
public void OnDisable()
{
inputActions.Disable();//禁用
}
}
?\
完成
?
?
?
?
?
?
?
到了這里,關(guān)于Unity簡(jiǎn)單操作:InputSystem獲取WASD鍵盤(pán)輸入 移動(dòng)人物的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!