如何根据用户输入旋转预制件中的多个RigidBodies



我目前正试图根据单独的输入在预制件中旋转不同的Rigidbody。我有它的工作,当每个Rigidbody有一个单独的脚本附加到它与指定的输入,但我需要将它们组合成一个"主控制";脚本,这样我就可以最终将玩家角色垂直分成两半,这样一个玩家可以控制左半部分,一个玩家可以控制右半部分的肢体。

工作代码只附着在一个肢体上,在这种情况下,是左二头肌。四肢之间唯一的变化是键盘输入和乘数变量。

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)

我如何着手让它工作?

我很确定你的问题是所有这些

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)

type
type


只需通过Unity检查器在暴露的字段中进行所有引用,并完全删除您的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>();

相关内容

  • 没有找到相关文章

最新更新