【发布时间】:2018-01-06 10:57:54
【问题描述】:
请原谅并纠正我在代码中的严重错误,我有点菜鸟。
我最近在随便写一个脚本来控制带有铰链电机的汽车,我想,我为什么不使用统一文档。我去把代码转移到我的项目中:
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
void Start() {
HingeJoint hinge = GetComponent<HingeJoint>();
JointMotor motor = hinge.motor;
motor.force = 100;
motor.targetVelocity = 90;
motor.freeSpin = false;
hinge.motor = motor;
hinge.useMotor = true;
}
}
我试图更改代码:
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;
}
}
}
之前有人建议我不要使用与对象相同的变量,因此它被称为 motorz。我还被告知不要在 start 函数中声明变量,否则我不能在其他函数中使用它,我也改变了它,但现在我收到一条错误消息,说 '=' 符号是意外的。我该如何解决这个问题?
编辑:忘了提到这个脚本是进入汽车的后轮,而不是实际的汽车对象。
【问题讨论】:
-
不能在函数外使用函数。在
WHEELSPIN脚本中,将hinge = GetComponent<HingeJoint>();移动到一个函数中。对motor = hinge.motor;和它下面的代码执行相同的操作,Update函数中的代码除外。 -
hinge.motor = float motorz;语法无效。 -
@Dennis_E 它部分有效,但我需要知道它是如何无效的。去除浮动有帮助吗?另外,现在我收到一条错误消息,说 motorz 不存在。
-
@DavZRazorbladesDJDavZ 我不知道您要做什么,所以我无法回答。但是浮动不允许在
spot。正如程序员所说,您不能在方法之外调用该代码。 -
这条线应该做什么?
hinge.motor = float motorz;因为它看起来像胡言乱语。