當我修復這篇文章中的代碼時,我收到了這個錯誤:Assets\Scripts\PlayerMovement.cs(18,9): error CS0029: Cannot implicitly convert type 'void' to 'System.Action<UnityEngine.InputSystem.InputAction.CallbackContext>'如何修復它?
這是我得到錯誤的代碼:
controls.Gameplay.Move_Left.performed = Left();
controls.Gameplay.Move_Right.performed = Right();
和
void Left()
{
rb.AddForce(leftrightForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
void Right()
{
rb.AddForce(-leftrightForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
(這里有一個類似的帖子,但沒有用)
請幫忙,我真的被卡住了,需要盡快完成!轉到這篇文章以獲取我的完整代碼!
uj5u.com熱心網友回復:
所以這里有兩個問題。
- 您應該將方法本身傳遞給事件委托,而不是呼叫方法的結果(即
null,因此沒有)。 - 你有錯誤的方法簽名。
錯誤訊息告訴您:
錯誤 CS0029:無法將型別“void”隱式轉換為“System.Action<UnityEngine.InputSystem.InputAction.CallbackContext>”
它本質上是說它需要一個需要 a 的方法CallbackContext,但你正試圖通過void。這意味著如上所述的 1 和 2。
因此,要修復一個,我們將您的代碼更改如下:
controls.Gameplay.Move_Left.performed = Left;
controls.Gameplay.Move_Right.performed = Right;
要修復 2,我們需要匹配System.Action<UnityEngine.InputSystem.InputAction.CallbackContext>簽名,如下所示:
public delegate void Action<in T>(T obj);
這意味著我們需要一個不回傳任何東西 ( void) 并接受 a T(在本例中CalllbackContext) 的方法。將您的方法修復為如下所示,我們得到以下資訊:
void Left(UnityEngine.InputSystem.InputAction.CallbackContext context)
{
rb.AddForce(leftrightForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
void Right(UnityEngine.InputSystem.InputAction.CallbackContext context)
{
rb.AddForce(-leftrightForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
現在你的代碼是固定的。
請注意,我不使用 Unity,但從錯誤訊息中理解了此資訊,因此仔細閱讀它們確實值得。
uj5u.com熱心網友回復:
在這個簡單的解決方案中,只需丟棄背景關系并呼叫Left()and Right():
controls.Gameplay.Move_Left.performed = _ => Left();
controls.Gameplay.Move_Right.performed = _ => Right();
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/485836.html
上一篇:unity,按鈕坐標
