如何根据用户输入在预制件中旋转多个刚体

How to rotate multiple RigidBodies in a prefab based on user input

我目前正在尝试根据单独的输入在预制件中旋转不同的 Rigidbody。当每个 Rigidbody 都有一个带有指定输入的单独脚本时,我可以正常工作,但我需要将它们组合成一个“主控制”脚本,这样我最终可以将玩家角色垂直分成两半,所以 1 名玩家在镜像网络 API.

中,可以控制 body 的左半边和 1 个玩家可以控制右半边的肢体

工作代码仅附加到一个肢体,在本例中为左二头肌。肢体之间唯一变化的是键盘输入和乘数变量。

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); 
    }
}

这是附加到我无法开始工作的完整 body 预制件的“主控制”脚本。

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");

将 return null 因为您要查找的组件称为 Rigidbody .. 而不是 LeftForearm2 等。请参阅 GetComponent(string type)

type
The type of Component to retrieve.


只需通过 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>();