前言
我目前正在制作一款游戲,玩家可以從第三人稱視角出發,四處走動以過渡到車輛。
我曾考慮過使用發射器/接收器型別設定,但我認為我簡化它的方式不正確。
我將第三方的資產用于使用輸入的控制器。我的計劃是從我想要控制的物件啟用/禁用適當的腳本和相機。我已經能夠禁用前一個控制器并啟用下一個控制器,盡管我無法回傳啟用,因為腳本顯然不再運行了。
問題/請求
我希望能夠在同一個游戲物件上的不同組件腳本中參考 pawn 的輸入組件腳本,然后能夠啟用/禁用上述組件,盡管問題是輸入控制器具有變數名稱(不同的名稱取決于每個棋子上的第三方)。
以下是我的設定方式:我有一個PlayerTransmitter處理打開和關閉事物的基礎知識。我嘗試在處理所有輸入的地方進行此操作,但我不想更改原始控制器腳本來查看此腳本。這是在一個空的游戲物件上并處理玩家的“狀態”(步行或在哪輛車中)。

在每個 pawn 游戲物件(行走的 pawnCleanerPawn和車輛TractorPawn)上,我添加了一個名為InputReceiver. 這最初是為了將輸入從PlayerTransmitter物件本身傳遞給實際的控制器。
現在,行走的 pawn 有一個名為AdvancedWalkerController(你可能知道我說的那個)的組件,它控制玩家的行走運動,而載具有一個名為的組件VehicleController,它控制載具如何移動和操縱。
CleanerPawn

TractorPawn

上面的兩張圖片顯示我InputReceiver在兩個棋子上使用了相同的組件。我的計劃是傳入每個 pawn 的輸入控制器(暫時命名為CleanerController),然后根據PlayerTransmitter“狀態”啟用/禁用該輸入控制器。
目前InputReceiver看起來是這樣的:
public bool isEnabled;
public Component CleanerController;
public void TurnOffControls()
{
isEnabled = false;
}
public void TurnOnControls()
{
isEnabled = true;
}
In each pawn's input controller, I added a line to the Update() function:
enabled = gameObject.GetComponent<InputReceiver>().isEnabled;. Two problems with this; 1. I don't want to change any code in the input scripts (I hope this is possible) and 2. Once it's disabled, it won't read an update to enable itself again.
I was hoping I could just use the reference to the component CleanerController to say CleanerController.enabled = true; or false with that reference being just that one component, though I'm missing something here.
My final thought which I am going to try is to allow/disallow the input controls within each input script depending on my isEnabled boolean. Though again, I would have to change the original scripts to accommodate this.
uj5u.com熱心網友回復:
反對什么
public Behaviour CleanerController;
接著
CleanerController.enabled = false; // or true accordingly
看
Behaviour行為是可以啟用或禁用的組件。
Behaviour.enabled啟用的行為會更新,禁用的行為不會。
或者換句話說:如果 a
Behaviour設定為,則不再呼叫enabled = false它(和類似的事件方法)。Update
uj5u.com熱心網友回復:
我建議你有一個控制器腳本。例如,這命名為“CameraController”,并具有在模式之間切換的方法。這也將有可能擁有更多的選項。例如,您可以使用 Enum 來定義存在哪些模式:
enum CameraMode {
Player,
Car
}
public SwitchMode(CameraMode mode) {
switch(mode) {
case CameraMode.Player:
TurnOnControls();
break;
case CameraMode.Car:
TurnOffControls();
break;
default:
throw new NotSupportedException();
}
}
你甚至可以更進一步,創建一個允許所有這些的介面/抽象,并且只有一個執行方法可以做不同的事情。最后,您只需要允許在模式之間切換并處理鍵輸入的控制器。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/424495.html
