我目前正在嘗試根據不同Rigidbody的輸入在預制件中旋轉不同的s。當每個Rigidbody腳本都有一個帶有指定輸入的單獨腳本時,我就可以使用它,但是我需要將它們組合成一個“主控制”腳本,這樣我最終可以將玩家角色垂直分成兩半,以便 1 個玩家可以控制左半部分并且 1 名玩家可以在鏡像網路 API 中控制身體的右半部分四肢。
作業代碼僅附加到一個肢體上,在這種情況下,是左二頭肌。肢體之間唯一改變的是鍵盤輸入和乘數變數。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LeftBicep : MonoBehaviour
{
public float amount = 6000f;
protected Rigidbody rb;
public float multiplier = 4f;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
float h = Input.GetAxis("leftbicep") * amount * Time.deltaTime;
GetComponent<Rigidbody>().AddTorque(Vector3.right * h * multiplier);
}
}
這是附加到我無法作業的全身預制件的“主控制”腳本。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour
{
public Rigidbody forearmL;
public Rigidbody forearmR;
public Rigidbody shoulderL;
public Rigidbody shoulderR;
public Rigidbody thighL;
public Rigidbody thighR;
public Rigidbody legL;
public Rigidbody legR;
public float amount = 6000f;
public float multiplier = 4f;
// Start is called before the first frame update
void Start()
{
forearmL = (Rigidbody)GetComponent("LeftForearm2");
forearmR = (Rigidbody)GetComponent("RightForearm2");
shoulderL = (Rigidbody)GetComponent("LeftBicep2");
shoulderR = (Rigidbody)GetComponent("RightBicep2");
thighL = (Rigidbody)GetComponent("LeftThigh2");
thighR = (Rigidbody)GetComponent("RightThigh2");
legL = (Rigidbody)GetComponent("LeftLeg2");
legR = (Rigidbody)GetComponent("RightLeg2");
}
void FixedUpdate()
{
float lBicep = Input.GetAxis("leftbicep") * amount * Time.deltaTime;
shoulderL.AddTorque(Vector3.right * lBicep * multiplier);
float lFArm = Input.GetAxis("leftforearm") * amount * Time.deltaTime;
forearmL.AddTorque(Vector3.right * lFArm * multiplier);
float lThigh = Input.GetAxis("leftthigh") * amount * Time.deltaTime;
thighL.AddTorque(Vector3.right * lThigh * multiplier);
float lLeg = Input.GetAxis("leftleg") * amount * Time.deltaTime;
legL.AddTorque(Vector3.right * lLeg * multiplier);
float rBicep = Input.GetAxis("rightbicep") * amount * Time.deltaTime;
shoulderR.AddTorque(Vector3.right * rBicep * multiplier);
float rFArm = Input.GetAxis("rightforearm") * amount * Time.deltaTime;
forearmR.AddTorque(Vector3.right * rFArm * multiplier);
float rThigh = Input.GetAxis("rightthigh") * amount * Time.deltaTime;
thighR.AddTorque(Vector3.right * rThigh * multiplier);
float rLeg = Input.GetAxis("rightleg") * amount * Time.deltaTime;
legR.AddTorque(Vector3.right * rLeg * multiplier);
}
}
使用場景中具有“主控制”腳本的玩家預制件運行游戲時遇到的例外是
NullReferenceException: Object reference not set to an instance of an object
Movement.FixedUpdate () (at Assets/Scripts/test/Movement.cs:38)
我該如何讓它發揮作用?
uj5u.com熱心網友回復:
我很確定你的問題是所有這些
forearmL = (Rigidbody)GetComponent("LeftForearm2");
forearmR = (Rigidbody)GetComponent("RightForearm2");
shoulderL = (Rigidbody)GetComponent("LeftBicep2");
shoulderR = (Rigidbody)GetComponent("RightBicep2");
thighL = (Rigidbody)GetComponent("LeftThigh2");
thighR = (Rigidbody)GetComponent("RightThigh2");
legL = (Rigidbody)GetComponent("LeftLeg2");
legR = (Rigidbody)GetComponent("RightLeg2");
將回傳,null因為您正在尋找的組件被稱為Rigidbody.. 不是LeftForearm2等,請參閱GetComponent(string type)
鍵入
該型別組件的檢索。
只需通過 Unity Inspector 在公開欄位中進行所有參考,然后Start完全洗掉您的方法!
或者,聽起來您傳入的GetComponent實際上是直接嵌套在主控制器下的子物件的名稱,因此您可以這樣做(但我不會那樣說)
// See https://docs.unity3d.com/ScriptReference/Transform.Find.html
forearmL = transform.Find("LeftForearm2").GetComponent<Rigidbody>();
forearmR = transform.Find("RightForearm2").GetComponent<Rigidbody>();
shoulderL = transform.Find("LeftBicep2").GetComponent<Rigidbody>();
shoulderR = transform.Find("RightBicep2").GetComponent<Rigidbody>();
thighL = transform.Find("LeftThigh2").GetComponent<Rigidbody>();
thighR = transform.Find("RightThigh2").GetComponent<Rigidbody>();
legL = transform.Find("LeftLeg2").GetComponent<Rigidbody>();
legR = transform.Find("RightLeg2").GetComponent<Rigidbody>();
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/339688.html
