因此,我正在嘗試使用新的輸入系統創建一個新的第一人稱移動系統,以便更輕松地支持游戲手柄,并且當我嘗試在FixedUpdate回圈中讀取 Vector2 的值時遇到問題,它只輸出(0,0)但如果我在一個InputAction.performed活動中閱讀它,它就會起作用。但是,我無法使用該事件,因為它不會在鍵盤輸入上重復并且不流暢。我看過這里鏈接的教程,最后它確實演示了您可以從外部事件中提取資訊。現在我的問題是我錯過了什么還是有其他方法可以做到這一點,我的代碼在下面找到
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.Assertions;
public class MovementEngine : MonoBehaviour
{
public InputMaster Input; // My input device here
public float currentSpeed = 2f;
public float walkingSpeed = 2f;
public float runningSpeed = 4f;
public Transform cameraTransform;
public CharacterController controller;
public InputAction Movement;
// Start is called before the first frame update
void Awake()
{
Input = new InputMaster(); // Creates new instance
}
void OnEnable()
{
Movement = Input.Player.Movement;
Movement.Enable(); // it is enabled
Input.Player.Interaction.performed = Interact;
Input.Player.Interaction.Enable();
}
private void Interact(InputAction.CallbackContext context)
{
Debug.Log("Interact");
}
void OnDisable()
{
Movement.Disable();
Input.Player.Interaction.Disable();
}
// Update is called once per frame
void FixedUpdate(){
Debug.Log("Movement: " Input.Player.Movement.ReadValue<Vector2>()); // doesn't work
}
}
uj5u.com熱心網友回復:
將值(在執行的事件中檢索)存盤在變數中,并在固定更新中使用該變數。
確保從取消的事件中重置變數(否則變數將保存從執行的事件中最后檢索到的值)。
您可以將輸入值直接讀入類變數中,如下所示。
// Value read from user input
//
private Vector2 movement;
private void Start()
{
SubscribeEvents();
}
private void SubscribeEvents()
{
// Read the value directly into our movement variable
//
Input.Player.Movement.performed = ctx => movement = ctx.ReadValue<Vector2>();
// Reset the movement variable in cancelled since we are no longer interacting
//
Input.Player.Movement.cancelled = ctx => movement = Vector2.zero;
}
private void FixedUpdate()
{
Debug.Log("Movement: " movement);
}
uj5u.com熱心網友回復:
我不久前解決了這個問題,但 StackOverflow 從未發布我的答案
我通過降級到 Unity InputSystem v1.0.0 來修復它,因為 v1.1.1 似乎不喜歡這個InputAction.ReadValue<>()功能。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/349300.html
下一篇:在打字稿中匯出webpack功能
