【问题标题】:How to rotate multiple RigidBodies in a prefab based on user input如何根据用户输入在预制件中旋转多个刚体
【发布时间】:2021-10-26 04:30:05
【问题描述】:

我目前正在尝试根据单独的输入在预制件中旋转不同的Rigidbodys。当每个Rigidbody 都有一个带有指定输入的单独脚本附加到它时,我让它工作,但我需要将它们组合成一个“主控制”脚本,这样我最终可以将玩家角色垂直分成两半,以便 1 个玩家可以控制左半边和 1 名玩家可以在 Mirror Networking 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)

我该如何让它发挥作用?

【问题讨论】:

    标签: c# unity3d rigid-bodies prefab


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2023-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-04
      • 2015-07-17
      相关资源
      最近更新 更多