请原谅并纠正我在我的代码中的错误,我是一个有点菜鸟。
我最近随便用一个脚本来控制带铰链电机的汽车,我想,为什么我不使用统一文档。我把代码转移到了我的项目中:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WHEELSPIN : MonoBehaviour {
HingeJoint hinge = GetComponent<HingeJoint>();
private JointMotor motor = hinge.motor;
hinge.useMotor = true;
hinge.motor = float motorz;
motorz.freeSpin = true;
private void Update()
{
if (Input.GetAxis("Vertical"))
{
motorz.force = 1000;
motorz.targetVelocity = 900;
}
}
}
我试图更改代码:
cart.update();
我之前被告知不要使用与对象相同的变量,因此它被称为motorz。我还被告知不要在start函数中声明变量,否则我不能在其他函数中使用它,我也改变了但是现在我收到一条错误消息,说'='符号是意外的。我该如何解决这个问题?
编辑:忘了提到这个剧本是进入汽车的后轮,而不是实际的汽车对象。
答案 0 :(得分:2)
在WheelSpin类中,您将在类的构造函数中指定铰链。但是,monobehaviours是由Unity引擎任意构造的(当Unity序列化类实例时,可能由于很多原因而发生)。相反,应该使用Start()和Awake()回调来初始化变量。
答案 1 :(得分:1)
我无法对此进行测试,因为我没有使用您正在使用的库,但这应该会有所帮助....
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WHEELSPIN : MonoBehaviour
{
HingeJoint hinge;
JointMotor motorz;
private void Awake()
{
hinge = GetComponent<HingeJoint>();
motorz = hinge.motor;
hinge.useMotor = true;
motorz.freeSpin = true;
}
private void Update()
{
if (Input.GetAxis("Vertical") > 0f)
{
motorz.force = 1000;
motorz.targetVelocity = 900;
}
}
}
答案 2 :(得分:1)
我猜您对 字段初始值设定项 感到困惑。但在解释字段初始化程序是什么之前,我应该从如何正确定义类开始。
c#中的一个类似于现实世界中的契约或模具。定义一个类后,可以使用它来创建实例(比如使用模具在现实世界中投射对象)。同一个类的每个实例都遵循类中定义的行为。它有一些字段来存储数据和方法来操作。
以下是通常如何定义类:
// Double slashes starts a one-line comment
// Comments will be **ignored** when the program is running
// So comments are actually memos for programmers
// Here the class is named as "Creature"
class Creature
{
// Here you can put some fields that will be used to store data
int _heathPoint;
// _healthPoint = 10; // Not permitted because this is not a field declaration
// Here you can define methods that can be operated with
public void DoSomething()
{
// A method can be empty, just do nothing.
}
}
您可能会在本课程中看到我定义了一个字段_healthPoint
和一个方法DoSomething
而没有其他内容。事实上,在&#34;范围&#34;该类(由class
关键字后的第一级弯曲括号所包围的区域),仅字段和方法声明是允许的(嗯,不完全是,您可以实际添加嵌套类但这超出了本Q&amp; A)的范围。
如果您创建类Creature
的新实例,则此实例将具有整数字段_heathPoint
和方法DoSomething
。但是它的领域的价值是什么?在上面的定义中,它没有关于初始值的方式,因此_healthPoint
将设置为默认值:0
表示int
1}}数据。
在大多数情况下,此类字段值默认值不是我们想要的。在我们的示例中,将Creature
实例的初始_healthPoint
设置为0
是没有任何意义的。我们希望我们希望初始_healthPoint
为100
。以下是我们如何做到这一点:
class Creature
{
int _heathPoint;
// Below is something new.
// Please note that although it looks like an ordinary method like DoSomething(),
// it lacks the return type and has the same name as the class name
// Such a method is called **constructor**
public Creature()
{
_healthPoint = 100;
}
public void DoSomething()
{
}
}
在这里您可以看到我们添加了一个新方法,该方法与其托管类具有相同的名称。这种方法称为构造函数。顾名思义,当您尝试构造类的实例时,会自动调用构造函数
但是,这并不意味着构造函数创建实例,它们实际上用于初始化实例。调用构造函数时,实例已经创建。但细节超出了范围。
您需要知道的是,每当Creature
的新实例创建时,_healthPoint
的构造函数都会将100
的值初始化为Creature
。
但是,如果我们在一个需要初始化的类中有一百个字段呢?在构造函数中逐个初始化它们会很繁琐且容易出错
所以字段初始化程序来救援。除了在构造函数中显式初始化字段之外,可以使用字段初始值设定项将初始值设置为声明的字段。以下是我们示例中的样子:
class Creature
{
// Here the = 100 part is the field initializer
int _heathPoint = 100;
// Each field can only have one initializer
// So the line below is wrong and will not compile
// _healthPoint = 30; // wrong because _heathPoint has been declared above
public Creature()
{
// Do not need to do anything because the field initializer does what we want
}
public void DoSomething()
{
}
}
您可能会在此处看到字段声明int _healthPoint;
变为int _healthPoint = 100;
。这意味着我们定义类的所有实例Creature
将有一个名为_healthPoint
的字段,我们希望将其值初始化为100
。
更有趣的是,只要它评估为声明字段的类型(在我们的例子中为=
),您可以在赋值运算符int
之后放置任何内容。
因此int _healthPoint = 100 + 20;
有效,int _healthPoint = Int.Parse("100");
也是如此。 (你可以尝试一下!)
回到原始代码,我看到的是您似乎尝试初始化字段(尤其是hinge
)。我希望从上面的解释中你现在明白hinge.useMotor = true;
是无效的,因为它不是字段声明,也不是字段初始值设定项的一部分。
好的,那么如果我们真的想要,我们如何初始化hinge
?请记住,字段初始值设定项可以是评估字段类型的任何内容吗?这意味着我们可以在方法中进行初始化,并使字段初始化程序调用方法,例如:
class ExampleClass : MonoBehaviour
{
HingeJoint hinge = InitializeHinge();
private HingeJoint InitializeHinge()
{
// Note that the line below compiles but may not run or will report runtime error
// I am not sure if you can call GetComponent() in field initializer
HingeJoint returnValue = GetComponent<HingeJoint>();
returnValue.useMotor = true;
return returnValue;
}
}
通过这种方式,一旦创建了ExampleClass
的实例,其字段hinge
将被初始化为HingeJoint
返回的GetComponent<HingeJoint>()
并获取其useMotor
1}}字段设置为true
。