我最近一直在学习C#。事情是:我有几个游戏对象有相同类型的运动,它们接收不同的输入,处于不同的层次,并被这些层上的多个敌人攻击,所以,为了省去一些繁琐的工作,我试图使用相同的动作脚本他们我有一个脚本巫婆估计字符串变量的不同文本,后来用于在移动脚本上给出一个键名。它显然不会在输入时给我错误,但它也不起作用,我不知道我想做什么是可能的,或者我写错了。我疯了,但它是合法的。
这里是InputScript:
public string forward;
public string backwards;
public string hold;
GameObject squadA;
GameObject squadB;
GameObject squadC;
GameObject squadD;
void Start () {
squadA = GameObject.Find ("Squad A");
squadB = GameObject.Find ("Squad B");
squadC = GameObject.Find ("Squad C");
squadD = GameObject.Find ("Squad D");
//A Input
if (gameObject == squadA) {
forward = "KeyCode.Alpha3";
backwards = "KeyCode.Alpha1";
hold = "KeyCode.Alpha2";
}
//B Input
if (gameObject == squadB) {
forward = "KeyCode.E";
backwards = "KeyCode.D";
hold = "KeyCode.W";
}
//C Input
if (gameObject == squadC) {
forward = "D";
backwards = "A";
hold = "S";
}
//D Input
if (gameObject == squadD) {
forward = "C";
backwards = "Z";
hold = "X";
}
}
运动一个(我不认为下半场是必要的,但以防万一(运动很奇怪,但它确实是我想要的那个)):
public float constantSpeed = 3;
float counterConstantSpeed = -3;
float speed;
float spawnpos;
public bool goingBackwardsOrStatic = true;
public bool holding =false;
KeyInput ScriptBeholderKI;
void Start () {
spawnpos = transform.position.x;
ScriptBeholderKI = gameObject.GetComponent <KeyInput> ();
}
//Key inputs
void Update () {
transform.Translate (constantSpeed * Time.deltaTime, 0, 0);
if (Input.GetKeyDown (ScriptBeholderKI.forward)) {
StopAllCoroutines ();
StartCoroutine (RightMovement(0f));
}
if (Input.GetKeyDown (ScriptBeholderKI.backwards)) {
StopAllCoroutines ();
StartCoroutine (LeftMovement(0f));
}
if (Input.GetKeyDown (ScriptBeholderKI.hold)) {
StopAllCoroutines ();
StartCoroutine (Hold(0f));
}
}
//Movement itself (Right, Left, hold)
IEnumerator RightMovement (float Rloop) {
while (transform.position.x < constantSpeed * Time.time + spawnpos + 16) {
goingBackwardsOrStatic = false;
speed = 10f;
transform.Translate (speed * Time.deltaTime, 0, 0);
yield return new WaitForSeconds (Rloop);
}
if (transform.position.x > constantSpeed * Time.time + spawnpos + 15.9) {
StopAllCoroutines ();
StartCoroutine (LeftMovement (0f));
}
}
IEnumerator LeftMovement (float Lloop) {
goingBackwardsOrStatic = true;
while (transform.position.x > constantSpeed * Time.time + spawnpos) {
speed = -7f;
transform.Translate (speed * Time.deltaTime, 0, 0);
yield return new WaitForSeconds (Lloop);
}
}
IEnumerator Hold (float Lloop) {
holding = true;
while (transform.position.x > constantSpeed *Time.time + spawnpos) {
transform.Translate (counterConstantSpeed * Time.deltaTime, 0, 0);
yield return new WaitForSeconds (Lloop);
}
}
这会让玩家稍后再犯以下错误:
ArgumentException:名为:KeyCode.Alpha3的输入键未知 UnityEngine.Input.GetKeyDown(System.String name)(在C:/buildslave/unity/build/artifacts/generated/common/runtime/UnityEngineInputBindings.gen.cs:533) ASquadMovement.Update()(在Assets / Scripts / Squad / Self behavoiurs / Squads movement / ASquadMovement.cs:26)
答案 0 :(得分:1)
您正在制作“KeyCode.Alpha3”和其他不存在的值。您可以使用enum
或string
执行此操作,但我认为enum
适用于此,因为它可确保KeyCode存在。
我在KeyInput
课程中,更改
public string forward;
public string backwards;
public string hold;
到
public KeyCode forward;
public KeyCode backwards;
public KeyCode hold;
然后用以下代码替换整个if语句:
//A Input
if (gameObject == squadA)
{
forward = KeyCode.Alpha3;
backwards = KeyCode.Alpha1;
hold = KeyCode.Alpha2;
}
//B Input
if (gameObject == squadB)
{
forward = KeyCode.E;
backwards = KeyCode.D;
hold = KeyCode.W;
}
//C Input
if (gameObject == squadC)
{
forward = KeyCode.D;
backwards = KeyCode.A;
hold = KeyCode.S;
}
//D Input
if (gameObject == squadD)
{
forward = KeyCode.C;
backwards = KeyCode.Z;
hold = KeyCode.X;
}
你需要在使用Unity之前学习C#,否则你会犯这样的简单错误。这是一个enum tutorial。