c# - 如何根据用户输入在预制件中旋转多个刚体
问题描述
我目前正在尝试Rigidbody
根据单独的输入在预制件中旋转不同的 s 。当每个都有一个带有指定输入的单独脚本附加到它时,我让它工作Rigidbody
,但我需要将它们组合成一个“主控制”脚本,这样我最终可以将玩家角色垂直分成两半,这样一个玩家就可以控制左半部分并且 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)
我该如何让它发挥作用?
解决方案
我很确定你的问题是所有这些
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
.. notLeftForearm2
等。请参阅GetComponent(string 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>();
推荐阅读
- node.js - 如何使用 mongodb 聚合计算数组字段的百分比?
- python - 张量流 cudaGetDevice() 失败。Status: cudaGetErrorString symbol not found.?
- php - PHP count div-s inside parent div,添加代码时取决于内部div-s摘要
- java - 尽管添加了 JavaFX 插件,Gradle 项目无法找到 JavaFX 应用程序类
- javascript - 以下 JS 代码不适用于函数中的 .length 属性
- excel - 从特定的 MS 访问字段中获取值
- javascript - 如何将同步结果返回给异步函数调用
- flutter - 当我尝试将项目添加到列表时,Flutter 编辑器显示错误
- list - 从 listTile 导航到不同的屏幕(颤振,飞镖)
- python - 如何在 dask 数据框中删除带有 nan 单元格的行?